jquery - Backbone and jqm: back button how to restore page context -
i'm building html5 mobile application using backbone, require , jquery mobile technology stack. application smoothy connection backend web service , on. change between pages use jquery changepage. instanciate backbone views use following strategy:
$( document ).delegate("#card-delivery-address", "pageshow", function(){ require(["js/views/carddeliveryaddressviews.js" ], function(carddeliveryaddressviews) { new carddeliveryaddressviews(); }); });
$.mobile.changepage('deliveryaddress.html') => changes current page using jquery mobile
when event called "pageshow" fired on #card-delivery-address (which means page inserted in dom , rendered) => create backbone view , bind $el existing dom , taking control on dom events using backbone views.
- to pass data , instances between views use window.tempdata global variable in put context data new view know do.
doing 1 way navigation successful, suppose come view1 --> view2 (with tempdata) view2 --> view 3 (override same tempdata). now, , here problem: if want go view 3 --> view 2 need tmpdata content used initialize , render view 2. same thing when want go view1.
note: i'm not using backbone router can change use if solve problem.
any ideas guys?
ok let's give try. we'll implement data store keyed pages' path.
var pagesdata = {}; // let's assume switched page '/my/page/1' // reference stored page data if exists, // or create empty , return it. var currentpagedata = pagesdata[window.location.pathname] || (pagesdata[window.location.pathname] = {}); currentpagedata.foo = 'bar'; console.log(pagesdata['/my/page/1'].foo); // > "bar"
now pages have "local" datastore allows them save state/data throughout navigation.
nb: if don't use pushstate, have use window.location.hash in place of window.location.pathname key in pagesdata.
Comments
Post a Comment