我發送了很多顧問的幫助,在我的問題部分解決了,但仍然存在一些問題。如何通過backbone.js中的全局變量創建集合?
我諮詢了答案,我試圖解決問題,因爲它我瞭解JavaScript命名空間pattren。
A namespacing pattern to avoid polluting the global namespace.
More details on this namespacing pattern
How do I declare a namespace in JavaScript?
我從問題的困擾,全球變量被創建成功但是,我不處理生成的變量。
collecton.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'被定義 – mikeapr4