【发布时间】:2017-07-11 00:59:06
【问题描述】:
stackoverflow 中的许多顾问向我发送了帮助,我的部分问题已解决,但仍有一些问题。
我查阅了答案,并尝试解决问题,因为我理解了 javascript 命名空间模式。
A namespacing pattern to avoid polluting the global namespace.
More details on this namespacing pattern
How do I declare a namespace in JavaScript?
我遇到了成功创建全局变量的问题,但是我不处理生成的变量。
colleton.js
var app = app || {};
(function () {
'use strict';
var collections = app.Collection = app.Collection || {};
collections.VideoLists = Backbone.Collection.extend({
model: app.Model,
initialize: function(){
console.log('load Collection');
}
});
app.Collection = collections.VideoLists;
})();
model.js
var app = app || {};
(function() {
'use strict';
var models = app.Model = app.Model || {};
models.Video = Backbone.Model.extend({
initialize: function(){
console.log('model create');
},
defaults:{
id : "1",
url : "/assets/videos/call/MOV01718.mp4",
imgSrc : "assets/img/call/1_thumbnail.png",
title: "call situation conservation"
}
});
app.Model = new models.Video();
})();
router.js
listRoute: function(id) {
//generate the collection using the listsection
var videoList = this.collection;
var getId = parseInt(id);
switch (getId) {
case 1:
new videoList([
{
id : "1",
url : "/assets/videos/call/MOV01718.mp4",
imgSrc : "assets/img/call/1_thumbnail.png",
title: "call situation conservation"
},
{
id : "2",
url : "/assets/videos/call/MOV01722.mp4",
imgSrc : "assets/img/call/2_thumbnail.png",
title: "call situation conservation"
}
]);
break;
//app.router init part
initialize: function() {
// create the layout once here
this.layout = new views.Application({
el: 'body',
});
// create model and collection once here
this.model = app.Model;
this.collection = app.Collection;
}
我认为生成已经正确完成。
但我不明白为什么会出现此错误。
我一开始试过
1. 不要使用new function 生成集合(作为当前我的来源)
2.创建变量videoList作为对象。
3. 将Collection 存储在一个变量中。
4.使用collection.create函数。
例如,list.create({id:'dasdsad'});
但是,这种尝试最终产生了相同的结果。
我该如何解决?
【问题讨论】:
-
您是否在 model.js 脚本之前包含了 collection.js?在 collection.js 中设置断点并确保定义了
app.Model