3

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

  1. 当我使用这样的导航功能时:

    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();
});
4

1 回答 1

1

使用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'); });
于 2013-05-11T08:33:19.073 回答