str*_*ght 5 javascript amd requirejs backbone.js marionette
我是新来Require.js,我试图做一些事情,我以为会很简单,但也开始有疼痛感.
我正在尝试为我的Backbone应用程序定义一个全局命名空间,并将其作为模块加载.这是我的命名空间(main.js):
define(
['jquery',
'underscore',
'backbone',
'GlobalRouter'
],
function($, _, Backbone) {
var App= {
Models: {},
Views: {},
Collections: {},
Routers: {},
init: function() {
new App.Routers.GlobalRouter();
Backbone.history.start();
}
}
return App;
});
Run Code Online (Sandbox Code Playgroud)
这是我的config.js文件:
require.config({
// your configuration key/values here
baseUrl: "js", // generally the same directory as the script used in a data-main attribute for the top level script
paths: {
'jquery' : '//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min',
'underscore': 'vendor/underscore-min',
'backbone': 'vendor/backbone-min',
'marionette': 'vendor/backbone.marionette',
'main' : 'main'
}, // set up custom paths to libraries, or paths to RequireJS plugins
shim: {
'underscore': {
exports: '_'
},
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
'main' : {
deps: ['underscore', 'jquery', 'backbone', 'GlobalRouter'],
exports: 'TEWC'
}
} // used for setting up all Shims (see below for more detail)
});
define([
'jquery',
'underscore',
'backbone',
'main'
],
function($, _, Backbone, App, GlobalRouter) {
console.log(App)
alert('hit ')
$(function() {
App.init();
});
}
Run Code Online (Sandbox Code Playgroud)
);
好的方法,这是我的路由器:
define([
'jquery',
'underscore',
'backbone',
'main'
],
function($, _, Backbone, TEWC) {
TEWC.Routers.GlobalRouter = Backbone.Router.extend({
routes: {
"" : "index",
"documents/:id" : "edit",
"login" : "login"
},
edit: function(id) {
alert('edit')
},
index: function() {
alert('index')
},
login: function() {
alert('login')
}
});
});
Run Code Online (Sandbox Code Playgroud)
在过去,我得到一个'app is undefined error'.现在,几分钟后我收到加载超时错误,说明:
Uncaught Error: Load timeout for modules: main
Run Code Online (Sandbox Code Playgroud)
但是,警报不会触发,并且main.js似乎没有加载,但我相信路由器确实如此,并且它没有吠叫TEWC未定义 - 所以它可能正在加载,即使它不在我的网络标签?
这可能是一个新手问题 - 有没有人对此有任何见解?
以下代码未定义 GlobalRouter,但它已传递给定义回调
define([
'jquery',
'underscore',
'backbone',
'main'
],
function($, _, Backbone, App, GlobalRouter) {
console.log(App)
alert('hit ')
$(function() {
App.init();
});
}
Run Code Online (Sandbox Code Playgroud)
添加GlobalRouterdefine
其次,当它加载失败时main..你可以从控制台检查它试图访问什么URL吗?这很可能是配置错误。
| 归档时间: |
|
| 查看次数: |
6902 次 |
| 最近记录: |