简体   繁体   中英

Backbone.js unable to access the router functions?

I have a simple implementation of Backbone.js which goes like this -

$(document).ready(function(){
    Workspace = Backbone.Router.extend({
    routes: {
    "/getAcademics": "academics"
        }, 

    academics: function(){
            alert("ok");
    $("#content").append("<div>Academics</div>");
    }
    }); 
var myWorkspace = new Workspace;
});

The link in the body is given as -

     <a href="#/getAcademics">Academics</a>

Though the router routes to the link #/getAcademics but it does not execute the function academics. Any clues as how to rectify this. Am I missing something else.

Thanks

After you instantiate your router, and before you attempt to use any of its routes, you need to call Backbone.history.start(). This will setup a listener for hash changes and call the correct route functions when needed.

You can find more information here: http://documentcloud.github.com/backbone/#History

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM