2012-05-10 23 views
5

我正在絆倒我的第一個骨幹項目,並試圖在單擊按鈕元素後更新URL。骨幹 - 更新來自查看功能的網址

我可以通過window.location.hash手動設置URL,但我認爲這不是正確的方法。誰能告訴我什麼是更新URL的首選方式?

var AppView = Backbone.View.extend({ 
    events: { 
     "click #loadProject": "filterProject", 
    }, 
    filterProject: function(){ 
     var projectID = $('#selectProject option:selected').val(); 
     var orgID = 'test'; 

     // Is there a better way to do this? 
     window.location.hash = '/'+orgID+'/'+projectID; 

     this.renderProject(orgID,projectID); 
    }, 
    renderProject:function(orgID,projectID){ 
    //Some code 
    }, 
}); 

//Routing 
var PropertiesRouter = Backbone.Router.extend({ 
    routes: { 
     "/:who/:project":"getProjects", //#/org/1 
     "/:who":"getOrganisation", //#/org 
     "*actions": "defaultRoute"   
    }, 
    getProjects:function(who,project){ 
     app.renderProject(who,project); 
    }, 
    getOrganisation:function(who){ 
     app.renderOrganisation(who); 
    }, 
    defaultRoute: function(actions){ 
     app.renderHomePage(); 
    }, 
}); 

var app = new AppView(); 
//create router instance 
var propertiesRouter = new PropertiesRouter(); 
//start history service 
Backbone.history.start(); 

謝謝!

+0

爲了防止其他人遇到此問題,解決方法是使用propertiesRouter.navigate(「/'+ orgID +'/'+ projectID +'」,{trigger:true}); – Matt

回答

8

你絕對可以打電話window.location.hash,但這確實打破了一些分離主幹試圖爲你創造。更好的選擇是致電router.navigate(..)。請參閱http://backbonejs.org/#Router-navigate

+0

感謝您的快速回復。我一直在使用router.navigate(),但錯過了觸發器:文檔中的true變量。再次感謝! – Matt