Trunk - refresh URL from view function

I stumble on my first backbone project and try to update the url after clicking a button element.

I can manually set the URL via window.location.hash, but I assume this is the wrong way. Can someone tell me what is the preferred way to update the 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();

Thank!

+5
source share
1 answer

You can definitely call window.location.hash, but this violates some of the dividing lines you are trying to create for you. Better to choose router.navigate(..). See http://backbonejs.org/#Router-navigate

+8
source

All Articles