这是我第一次使用 backbone 的 require.js,我正在努力寻找我的观点的问题:
Cannot read property 'View' of undefined // search.js:8
我的目录结构是:
.
├── index.php
└── js
├── app.js
├── lib
│ ├── backbone.js
│ ├── backbone-min.js
│ ├── jquery-min.js
│ ├── require.js
│ └── underscore-min.js
├── main.js
├── model
├── router.js
├── text.js
└── view
├── error.js
└── search.js
我的main.js
:
require.config({
paths: {
jquery: 'lib/jquery-min',
underscore: 'lib/underscore-min',
backbone: 'lib/backbone-min',
templates: '../templates'
}
});
require([
'app'
], function(App){
App.initialize();
});
我的app.js
:
define([
'jquery',
'underscore',
'backbone',
'router', // Request router.js
], function($, _, Backbone, Router){
var initialize = function(){
// Pass in our Router module and call it's initialize function
Router.initialize();
}
return {
initialize: initialize
};
});
我的router.js
:
define([
'jquery',
'underscore',
'backbone',
'view/search', // requests view/search.js
], function($, _, Backbone, SearchView){
var AppRouter = Backbone.Router.extend({
routes: {
"": "home"
}
});
var initialize = function(){
var app_router = new AppRouter;
app_router.on('route:home', function(){
var homeView = new SearchView();
homeView.render();
});
Backbone.history.start();
};
return {
initialize: initialize
};
});
和我的view/search.js
:
define([
'jquery',
'underscore',
'backbone',
'text!templates/search.html'
], function($, _, Backbone, searchTemplate){
// console.log($,_,Backbone);
var SearchView = Backbone.View.extend({
...
});
return SearchView;
});
当我取消注释上面的 console.log 时,_
和 Backbone
都是 undefined
但 $
不是吨。我错过了什么?我所有的lib文件都是最新版本。
最佳答案
Backbone 和 Underscore 不符合 AMD 标准。默认情况下,它们不提供与 RequireJS 兼容的接口(interface)。
在上一个版本中,RequireJS provide a useful way to solve the problem :
你的 main.js:
require.config({
shim: {
underscore: {
exports: '_'
},
backbone: {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
}
},
paths: {
jquery: 'lib/jquery-min',
underscore: 'lib/underscore-min',
backbone: 'lib/backbone-min',
templates: '../templates'
}
});
require([
'app'
], function(App){
App.initialize();
});
关于javascript - 无法读取未定义的属性 'View',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13944672/