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

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

    What about this approach? As backbone implements the template pattern in all its 4 components, with a little of design you can provide to each view an easy navigation mechanism through the app's router without needing to make any circular reference (this was something I saw in other similar posts, but try to avoid it).

    Router component, not to much different from other router examples:

    define('Router', ['backbone', ... ],
            function (Backbone, ...) {
    
                var MyRouter = Backbone.Router.extend({
                    routes: {
                        'viewA': 'viewA',
                        'viewB': 'viewB'
                    },
    
                    initialize: function () {
                        ...
                    };
                },
                viewA: function () {
                    ...
                },
    
                viewB: function () {
                    ...
                }
    });
    
    return MyRouter;
    });
    

    App, creates the router instance and fires the first view passing this instance:

    define('App', ['backbone', ...
    ], function (Backbone, ...) {
    
        function initialize() {
    
            //route creation
            if (!this.routes)
                routes = new Router(this); 
            //backbone history start
            Backbone.history.start();
    
            //ViewA navigation, bigbang
            if (!this.viewA)
                this.viewA = new ViewA({router: this.routes});
            this.viewA.render();
        }
    
        return {
            initialize: initialize
        };
    });
    

    BaseView, base constructor definition for all views and also a navigation method:

    define('BaseView', ['jquery', 'underscore',  'backbone', ...
    ], function ($, _, Backbone, ...) {
        var BaseView;
    
        BaseView = Backbone.View.extend({
            id: '...',
    
            constructor: function (options) {
                this.router = options.router;
                this.model = options.model;
                Backbone.View.prototype.constructor.call(this);
            },
            initialize: function () {
                this.template = _.template(tpl);
            },
    
            events: {
    
            },
            render: function () {
                $(this.el).html(this.template());
    
                return this;
            },
            //Provides transparent navigation between views throught the backbonejs
            //route mechanism
            navigate: function(pageId)
            {
                this.router.navigate(pageId, {trigger: true});
            }
        });
    
        return BaseView;
    });
    

    A View instance, each view extends from the base one instead of backbone, and inherits base behavior:

    define('ViewA', ['jquery', 'underscore',  'backbone', 'BaseView'
    ], function ($, _, Backbone, BaseView) {
        var ViewA;
    
        ViewA = BaseView.extend({
            id: '...',
    
            constructor: function (options) {
                this._super("constructor");
            },
    
            ...
            foo: function()
            {
                ...
    
                this.navigate("viewB");
            }
        });
    
        return ViewA;
    });
    

    It works for me, and also it can be reuse in other projects.

提交回复
热议问题