0

我正在使用 Backbone.js require.js underscore.js 来构建应用程序。在我的 javascript 中,我有处理菜单选择的 mainrouter.js 文件。

该应用程序分为几个部分,我希望每个部分都有一个 sectionRouter.js 文件。

出于某种原因,当尝试加载一个 sectionRouter.js 时,我收到以下错误

类型错误:SectionRouter 未定义

这是我的 mainApplication.js 文件的代码

define([ 
     'jquery', 
     'underscore', 
     'backbone', 
     'mainRouter', // Request mainRouter.js 
     'bootstrap',
     'sectionRouter'  // this is the
 ], function($, _, Backbone, Router,SectionRouter){ 
var initialize = function(){ // Pass in our Router module and call it's initialize function
    Router.initialize();
    initializeAnalytics();
    //Initiate a new history and controller class
    Backbone.emulateHTTP = true;
    Backbone.emulateJSON = true;           
    Backbone.history.start();                
}; 

var initializeAnalytics = function(){
    SectionRouter.initialize();
};

var initializeAll = function(){
    initialize();
    initializeAnalytics();
};

return { 
    initialize: initialize
};  
}); 

sectionRouter的一部分:

 var SectionRouter= Backbone.Router.extend({
            //restfulUrl:"http://localhost:8080/myapp/", //This is the application service 
                            //Routes tell the app what to do
            routes:{
                "analytics/consumptions":"consumptionsActions",
                "analytics/contents":"contentsActions",
                "analytics/users":"usersAction"                    
            }               
        });

 var initialize = function (){              
            var sectionConsoleRouter= new SectionRouter;                
            //map consumptionsAction routing
            sectionConsoleRouter.on('route:consumptionsActions', function(){                    
                SeriesStorage(url to call);                  
             });
   };

  return { 
            initialize: initialize
        }; 

你能告诉我是否可以一起加载 mainRouter.js 和其他 sectionRouter.js 文件。

编辑

是否可以在 mainRouter 中初始化 sectionRouter?

4

1 回答 1

0

You've added bootstrap to your list of define function parameters, but the list doesn't match the modules in your require array. So SectionRouter doesn't have a value (which is why you're getting "undefined").

Try:

define([ 
     'jquery', 
     'underscore', 
     'backbone', 
     'mainRouter', // Request mainRouter.js 
     'sectionRouter'  // this is the
     'bootstrap'
 ], function($, _, Backbone, Router,SectionRouter, Bootstrap){ 

There are probably other things to improve what you're trying to achieve but this will answer the question you've asked here.

于 2013-03-20T09:46:16.970 回答