Routing in SAPUI5: How to implement passing of URL? Model data not initialy loaded -
my goal write sapui5 fiori app routing support. 1 mail goal have passable urls. example in e-mail "please approve this: link". link url matched rounting config, e.g.index.html#/applicants/8
.
i use typical sap.m.splitapp kind of application. clicking list item in masterview changes url index.html#/applicants/[id of entry in json]
. can click on list, defined routes getting matched , apps loads (applicant) data expected.
however, , here comes question, doeas not work when using url directly, pasting [my url]/index.html#/applicants/8
browser. app launched no detail data loaded. have click on list item again data.
actually, controller called when passing url, seems model not initiated , undefined. json model bound in createcontent
function of component.js
// update 2015-05-14 problems seems around getdata()
function. have model, has entries, getdata()
returns undefined first time app loaded. read getdata()
deprecated. how should improve coding below?
// component.js ui5testing.component.prototype.createcontent = function(){ // create root view var oview = sap.ui.view({ id : "app", viewname : "ui5testing.view.main", type : "js", viewdata : { component : } var omodel = new sap.ui.model.json.jsonmodel("model/mock_applicants.json"); oview.setmodel(omodel); [...] return oview; }); // master controller handleapplicantselect : function (evt) { var ohashchanger = sap.ui.core.routing.hashchanger.getinstance(); var context = evt.getparameter("listitem").getbindingcontext(); var path = context.getpath(); var model = this.getview().getmodel(); var item = model.getproperty(path); ohashchanger.sethash("applicants/" + item.id); }, // detail controller oninit: function() { this.router = sap.ui.core.uicomponent.getrouterfor(this); this.router.attachroutepatternmatched(this._handleroutematched, this); }, _handleroutematched : function(evt){ var objectid = evt.getparameter("arguments").id; var model = this.getview().getmodel(); var data = model.getdata()["applicants"]; var pathid; if (data) { (var = 0; data.length; i++) { if ( objectid == data[i].id ) { pathid = i; break; } } var spath = "/applicants/" + pathid; var context = new sap.ui.model.context(model, spath) this.getview().setbindingcontext(context); } },
as you've figured out getdata() returns undefined first time, means model data still not yet loaded. can make use of attachrequestcompleted method of model & fire event component & listen event in detail controller ensure routerpatternmatched() gets executed after data loaded.
//component.js
var omodel = new sap.ui.model.json.jsonmodel("model/mock_applicants.json"); omodel.attachrequestcompleted(jquery.proxy(function(){ this.fireevent("mockdataloaded"); // fireevent through component },this)); oview.setmodel(omodel);
//detail controller
oninit : function(){ this.router = sap.ui.core.uicomponent.getrouterfor(this); var ocomponent = this.getownercomponent(); ocomponent.attachevent("mockdataloaded",jquery.proxy(function(){ this.router.attachroutepatternmatched(this._handleroutematched, this); },this)); }
or simplest & dirty way make synchronous request instead of async request load data.
var omodel = new sap.ui.model.json.jsonmodel(); omodel.loaddata(""model/mock_applicants.json",{basync:false}); oview.setmodel(omodel);
Comments
Post a Comment