RequireJS 2.0-定义我的Backbone.Return

RequireJS 2.0 - Defining my Backbone.Router

本文关键字:Backbone Return 我的 定义 RequireJS      更新时间:2024-03-11

我目前正在尝试RequireJS 2.0,因此必须更改一些代码。这是我的main.js,在我的index.phtml中与data-main一起发布。

编辑:忘记项目结构,可能很有用:)

->public
 -> css
 -> js
 app.js
 facade.js
 main.js
 router.js
 ...
  -> collections
  -> libs
   -> backbone
   -> jquery
   -> json2
   -> plugins
   -> require
   -> underscore
 -> templates
...

// Filename: main.js
// Require.js allows us to configure shortcut alias
// There usage will become more apparent futher along in the tutorial.
require.config({
    //Here baseUrl = /js (Loaded in data-main in logged.phtml)
    // 3rd party script alias names (Easier to type "jquery" than "libs/jquery-1.7.2.min")
    paths: {
        jQuery: 'libs/jquery/jquery',
        Underscore: 'libs/underscore/underscore',
        Backbone: 'libs/backbone/backbone',
        JSON: 'libs/json2/json2',
        templates: '../templates'
    },
    // Sets the configuration for your third party scripts that are not AMD compatible
    shim: {
        'libs/plugins/bootstrap-min': ['jQuery'],
        'libs/plugins/jquery.cookies.min': ['jQuery'],
        'jQuery': {
            exports: '$'
        },
        'JSON': {
            exports: 'JSON'
        },
        'Underscore': {
            exports: '_'
        },
        'Backbone': {
            deps: ['Underscore', 'jQuery'],
            exports: 'Backbone'  // attaches "Backbone" to the window object
        }
    }
});
define([
    // Load our app module and pass it to our definition function
    'app',
    'jQuery',
    'Underscore',
    'Backbone',
    'JSON'
], function(App) {
    // The "app" dependency is passed in as "App"
    // Again, the other dependencies passed in are not "AMD" therefore don't pass a parameter to this function
    App.initialize();
});

我试图将"router"及其依赖项添加到这个文件中,但没有任何更改,所以我认为它毫无用处。

以下是调用app:时加载的app.js

define([
    'jQuery',
    'Underscore',
    'Backbone',
    'JSON',
    'router', // Request router.js
    'i18n!nls/langs',
    'facade',
    //'order!libs/plugins/jquery.backstretch.min'
    //'libs/plugins/backbone.validation.min'
], function($, _, Backbone, JSON, Router, langs, facade) {
var initialize = ...
...
...
return {
        initialize: initialize
    };
});

问题是,在我的router.js定义中,我使用了console.log,它向我表明它返回了正确的对象。

但是当我在app.js中调用router时,router保持未定义状态。我可能犯了一个错误,但我找不到。

编辑:添加router.js代码

require([
    'jQuery',
    'Underscore',
    'Backbone',
    'JSON',
    'facade',
    'models/UserModel',
    'libs/plugins/jquery.cookies.min'
], function($, _, Backbone, JSON, facade, User, cookies){
    var AppRouter = Backbone.Router.extend({
...
    });
    var app_router = new AppRouter;
        // Start Backbone history a neccesary step for bookmarkable URL's
        Backbone.history.start();
    return app_router;
});

您的router.js文件没有正确定义AMD模块。您需要使用define,而不是require