많은 조언자가 stackoverflow에서 도움을 보냈습니다. 문제가 해결되었지만 몇 가지 문제가 남아 있습니다.backbone.js에서 전역 변수를 통해 컬렉션을 만드는 방법은 무엇입니까?
나는 대답을 참조하고 나는 그 결과로 문제를 해결하려고 노력했다. 자바 스크립트 네임 스페이스 패턴을 이해했다.
How do I declare a namespace in JavaScript?
More details on this namespacing pattern
A namespacing pattern to avoid polluting the global namespace. 나는 전역 변수가 성공적 그러나 만들어 졌는지, 내가 생성 된 변수를 처리하지 않는 문제로 고통.
가 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;
}
생성이 제대로 완료되었다고 생각합니다.
하지만 왜이 오류가 발생하는지 이해할 수 없습니다.
2. Object로서 변수
videoList
만들기 (현재 내 소스로)
new function
과 모음을 생성하지 않습니다 첫째
1에서 시도했다.
3.
Collection
을 변수에 저장합니다.
4. collection.create 함수를 사용하십시오.
예를 들어이
list.create({id:'dasdsad'});
그러나이 시도는 결국 같은 결과를 얻었다.
어떻게 해결할 수 있습니까?
* model.js * 스크립트 앞에 * collection.js *가 포함되어 있습니까? * collection.js *에 중단 점을 지정하고'app.Model'이 정의되어 있는지 확인하십시오 – mikeapr4