Using the Backbone.js router to navigate through views modularized with require.js

前端 未结 8 922
别跟我提以往
别跟我提以往 2020-12-22 23:41

I am separating my views and router into separate files with require. I then have a main.js file that instantiates the router, and also renders my default view.

My r

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-23 00:40

    In case anyone else is looking for a solution to this problem like I was, I'm posting what I ended up doing. If you're using the boilerplate backbone.js, then you will have an initialize() function in router.js. I modified my initialize() function to look like the following:

    initialize = function () {
      var app_router;
      app_router = new AppRouter();
    
      // Extend the View class to include a navigation method goTo
      Backbone.View.goTo = function (loc) {
        app_router.navigate(loc, true);
      };
    
      Backbone.history.start();
    };
    

    Due to backbone.js's particular flavour of inheritance, this allows allows me to call MyView.goTo(location); from within any of my views.

提交回复
热议问题