backbone.js - Using query strings in backbone (1.0.0) -


i have situation here can't seem figure out. please if knows how resolve love hear suggestions.thanks

i have "global view" visible on subnavbar in app, calendar, calendar serves global calendar throughout application, views use calendar view & model set show data according date selected.

this calendar view/model should have way store in history each time date changed, (i think) done using single url or query string parameters each time date changed, like

webapp/view1?date=20120301 

and when date changed, query string.

i use query string parameters don't have specify on each route (/:date) optional parameter.

the thing backbone stopped firing route change or history event when query strings changed, ignore query strings on backbone.history implementation, breaking implementation cause can't track each time querystring changed, "back" button not fire change event , therefore can't change date on model change date on calendar.

i know simple solution using "pretty url" , adding date parameter each view, im trying avoid that.

any suggestions? in advance


update

i ended using "pretty urls" backbone documentation suggests, cause using query strings bring me lot of trouble tracking url change , history, wouldn't work expected when using hashchange instead of pushstate.

so, code ended this:

attaching somewhere in router, view, controller, something, "route" event of router, check uri date , set date calendar picker:

this.listento(myrouter, "route", this.routechanged); 

then, method like:

checkuridateparameter: function (route, arr) {         var found = false;          (var = 0; < arr.length ; i++) {             if (arr && arr[i] && arr[i].indexof("date=") != -1) {                 if (app.models.datecontrol) {                     var datefromparameter = new date(arr[i].substring("date=".length).replace(/\$/g, ":"));                      datefromparameter = (isnan(datefromparameter.gettime())) ? app.models.datecontrol.defaults.date : datefromparameter;                     app.models.datecontrol.set("date", datefromparameter);                     found = true;                  }             }         }         if (!found) app.models.datecontrol.set("date", app.models.datecontrol.defaults.date, {changeuri:false});      } 

this serves function read params uri , reflect changes on datecontrol.

there should method 1 in charge of updating uri new 1 each time date changed (so params in sync view) , link can copied , pasted no problems.

somewhere on router, view, controller:

this.listento(app.models.datecontrol, "change:date", this.updateuridateparameter);  

this method attached model has current date, model updated calendar picker (the ui control) or method linked route event (routechanged, above).

this method should this:

, updateuridateparameter: function (a, b, c) {         if (c && c.changeuri == false) return; //this in case don't want refresh uri case default date set.           var currentfragment = backbone.history.fragment;         var newfragment = "";         var dateencoded = app.models.datecontrol.get("date").tojson().replace(/\:/g, "$");         newfragment = currentfragment.replace(/\/date=([^/]+)/, "") + "/date=" + dateencoded;           if (newfragment != currentfragment) {             app.router.navigate(newfragment, false);         }     } 

this method gets currentdate selected corresponding model, parses it, takes url backbone.history.fragment, execs nice regexp against replace old date parameter (if exists) , appends new encoded date. navigates route in silent mode method checks route not called (this prevents annoying loops).

i hope helps.

i suggest using "pretty url".

fyi page url in browser bar blink in example.

somewhere inside backbone.router:

this.route('*action', function() {   console.log('404'); });  this.route(/^(.*?)\?date=(\d+)$/, function(route, date) {   // same current route (with ?date)   var fragment = backbone.history.fragment;   // navigate main route (to create views etc.)   this.navigate(route, {trigger:true, replace:true});   // silent change hash   this.navigate(fragment); });  this.route('any', function() {   // need wait hash change   _.defer(function() {     console.log('parse date here , want', window.location.hash.match(/^(.*?)\?date=(\d+)$/));   }); });  this.route('route', function() {   // need wait hash change   _.defer(function() {     console.log('parse date here , want', window.location.hash.match(/^(.*?)\?date=(\d+)$/));   }); }); 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -