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

前端 未结 8 960
别跟我提以往
别跟我提以往 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:39

    for me i added an object to the main application like so;

    define(['jquery','underscore','backbone','views/personView','views/peopleView','views/textView'],function($,_,backbone,PersonView,PeopleView,TitleView){
    
        var Router = Backbone.Router.extend({
               routes:{
                   '':'home',
                   'edit/:id':'editPerson',
                   'new':'editPerson',
                   'delete/:id':'deletePerson'
                   }
                })
    
                  var initialize = function(){
    
                     var router  = new Router();
    
                     window.app = {
                         router:router
                         }
    
            router.on('route:home',function(){
    
    
        })
    
                //enable routing using '#'[hashtag] navigation
            Backbone.history.start();
    
                };
    
                return {
                initialize:initialize
                };
    
    });
    

    and inside your view, you can say windows.app.router.navigate({'',trigger:true}) . Don't know if global scoping is good practice in this case, but it worked for me.

提交回复
热议问题