1

我目前正在尝试 RequireJS 2.0,因此必须更改一些代码。这是我的main.jsdata-main在我的index.phtml.

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

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

我试图将“路由器”及其依赖项添加到该文件中,但没有任何改变,所以我认为它没用。

这是调用 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定义中,我使用了 aconsole.log并且它向我表明它正在返回正确的对象。

但是当我打电话给router我时app.jsrouter保持未定义。我可能犯了一个错误,但我找不到它。

编辑:添加了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;
});
4

1 回答 1

3

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

于 2012-07-30T18:38:46.760 回答