我正在尝试按照此处的说明在多页项目中使用RequireJS:https://github.com/requirejs/example-multipage
我有一个页面main.html ...
<html>
<head>
<script src="/components/require/require.js"></script>
<script>
require([ "/common.js" ], function ( common ) {
require([ "/app/views/main.js" ]);
});
</script>
</head>
<body />
</html>
我有一个模块main.js ...
define(function( require ) {
var $ = require( "jquery" );
var _ = require( "underscore" );
var Backbone = require( "backbone" );
$(function() {
var MainView = Backbone.View.extend({
initialize: function() {
alert( "hah!" );
}
});
new MainView();
});
});
此代码执行正常。到现在为止还挺好。我遇到的麻烦是当我尝试在主视图中需要另一个子视图时。
所以如果我改变main.js ......
define(function( require ) {
var $ = require( "jquery" );
var _ = require( "underscore" );
var Backbone = require( "backbone" );
var SubView = require( "/app/views/subview" );
$(function() {
var MainView = Backbone.View.extend({
initialize: function() {
this.subView = new SubView();
}
});
new MainView();
});
});
如果我添加一个subview.js ...
define(function( require ) {
var $ = require( "jquery" );
var _ = require( "underscore" );
var Backbone = require( "backbone" );
$(function() {
var SubView = Backbone.View.extend({
initialize: function() {
alert( "SubView Hah!" );
}
});
return SubView;
});
});
突然requirejs返回错误:&#34; require.js:8错误:脚本错误:views / subview.js&#34;
根据Require JS文档,此错误通常由语法错误/拼写错误(http://requirejs.org/docs/errors.html#scripterror)触发。但是,我不认为这是我的问题。
有谁知道我做错了什么?
答案 0 :(得分:0)
目前您的main.js
SubView
目标是undefined
,这是因为您的子视图未返回任何内容。
define(function(require)
{
// These 2 require calls are not needed since they are not "required" for this module.
//var $ = require( "jquery" );
//var _ = require( "underscore" );
var Backbone = require("backbone");
return Backbone.View.extend({
initialize: function()
{
alert( "SubView Hah!" );
}
});
});