browser history - Which one should I use? Backbone.js Router.navigate and window.location.hash -


i began learning backbonejs recently, reading book. , feel little bit confuse issue.here router:

define(['views/index', 'views/login'], function(indexview, loginview) {      var selinkrouter = backbone.router.extend({          currentview: null,          routes: {             'home': 'home',             'login': 'login'         },          changeview: function(view) {             if(null != this.currentview)                 this.currentview.undelegateevents();             this.currentview = view;             this.currentview.render();         },          home: function() {             this.changeview(indexview);         },          login: function() {             this.changeview(loginview);         }     });      return new selinkrouter(); }); 

and boot method of application:

define(['router'], function(router) {      var initialize = function() {         // require home page server         $.ajax({             url: '/home',          // page url             type: 'get',           // method             datatype: 'json',      // use json format             success: function() {  // success handler                 runapplicaton(true);             },             error: function() {    // error handler                 runapplicaton(false);             }         });     };      var runapplicaton = function(authenticated) {          // authenticated user move home page         if(authenticated) window.location.hash='home';                           //router.navigate('home', true); -> not work          // unauthed user move login page         else window.location.hash='login';              //router.navigate('login', true); -> not work          // start history         backbone.history.start();     }      return {         initialize: initialize     }; }); 

my question runapplication part. example of book read passed router module this, used window.location.hash = "xxx", , router wasn't touched @ all.

i thought "navigate" method make browser move page specified, nothing happened. why?

and best practice sake, best way achieve movement between pages(or views)?

thanks ideas.

according documentation, if want call function belonging specific route need pass option trigger: true:

whenever reach point in application you'd save url, call navigate in order update url. if wish call route function, set trigger option true. update url without creating entry in browser's history, set replace option true.

your code should like:

if(authenticated)     router.navigate('home', {trigger: true}); 

once router created, have call

backbone.history.start(); 

backbone.history.start([options])

when of routers have been created, , of routes set properly, call backbone.history.start() begin monitoring hashchange events, , dispatching routes.

finally runapplication logic similar this:

var runapplicaton = function(authenticated) {      var router = new selinkrouter();     // start history     backbone.history.start();      // authenticated user move home page     if(authenticated)         router.navigate('home', true);     // unauthed user move login page     else         router.navigate('login', true); } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -