我正在使用requirejs和骨干开发应用程序,在索引视图(索引页面)中,我遇到了2个困难...

当我使用如下导航功能时,

  • :

    this.navigate(“dashBoard /”);

  • 如果我使用的话,它是行不通的:
    Backbone.history.navigate("dashBoard/");
    

    工作正常。即使在我的“路线”中,我也没有触发该功能。这是我的索引视图。
    define(["singleton","backbone"],function (obj,Backbone) {
    
        window.EDMS = obj || {};
    
        EDMS.index = Backbone.View.extend({
            el:$(".loginPage > section"),
            events:{
                "click #loginBtn" : "enter"
            },
            initialize:function(){
                this.$el.html(this.template());
            },
            enter:function(e){
                e.preventDefault();
                var userName = $.trim(this.$el.find("#userName").val()),
                    password = $.trim(this.$el.find("#password").val());
    
                if(userName && password){
                    Backbone.history.navigate("dashBoard/"); //only this is works
                         //this.navigate("dashBoard/") not working.
                }else{
                    return false;
                }
            }
        });
    
        return EDMS.index;
    })
    

    但是在控制台“EDMS.routers”的情况下-我正在使用方法...如何将索引视图正确同步到路由器..有什么帮助吗?

    我的routers.js
    define(["singleton","backbone","utils"], function (EDMS,Backbone,utils) {
    
        window.EDMS = window.EDMS || {};
    
        EDMS.routers = Backbone.Router.extend({
            routes:{
                "":"caller",
                "dashBoard/":"dashBoard"
            },
            initialize:function(){
                utils(["index"]);
    
            },
            caller:function(){
                console.log("i am called");
            },
            dashBoard:function(){
                console.log("welcome to dashBoard");
            }
        });
    
        return EDMS.routers;
    
    })
    

    我的应用程序在require.js上初始化
    require(["jquery","underscore","backbone","singleton","routers"],function ($,_,Backbone,EDMS,routers) {
        EDMS.router = new EDMS.routers();
        Backbone.history.start();
    });
    

    最佳答案

    使用requirejs使您能够分离模块(您可能已经知道)。正确的方法是通过require访问分离的模块。

    当然,您的应用程序中应该包含以下内容:

    require.config({
      paths: {
        jquery: 'libs/jquery/jquery',
        underscore: 'libs/underscore/underscore',
        backbone: 'libs/backbone/backbone'
      }
    });
    

    您的路由器会在最终的应用初始化中初始化,并且基本上,代码应如下所示:
    define('routers', ["backbone","utils"], function (Backbone,utils) {
    
        var routers = Backbone.Router.extend({
            routes:{
                "":"caller",
                "dashBoard/":"dashBoard"
            },
            initialize:function(){
                utils(["index"]);
    
            },
            caller:function(){
                console.log("i am called");
            },
            dashBoard:function(){
                console.log("welcome to dashBoard");
            }
        });
    
        return new routers();
    
    });
    

    在您看来:
    define('indexView', ["routers","backbone"],function (router,Backbone) {
    
        var index = Backbone.View.extend({
            el:$(".loginPage > section"),
            events:{
                "click #loginBtn" : "enter"
            },
            initialize:function(){
                this.$el.html(this.template());
            },
            enter:function(e){
                e.preventDefault();
                var userName = $.trim(this.$el.find("#userName").val()),
                    password = $.trim(this.$el.find("#password").val());
    
                if(userName && password){
                    router.navigate("dashBoard/");
                }else{
                    return false;
                }
            }
        });
    
        return index;
    });
    

    并在您初始化时
    require(["jquery","underscore","backbone","indexView","routers"],function ($,_,Backbone,indexView,routers) {
        window.App = { indexView: new indexView() };
    });
    

    这样,您将在路由器中弹出一个事件,您可以从以下任何集合/视图/模型中监听该事件:
        router.on('route', function() { console.log('we have routed'); });
    

    关于backbone.js - Backbone 导航不被路由器监听,但在加载时有效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16494797/

    10-09 16:18
    查看更多