Ich werde die Hilfe von vielen Beratern in stackoverflow geschickt, in Teil meines Problems ist gelöst, aber ein paar Probleme sind geblieben.Wie erstelle ich Sammlung durch globale Variable in backbone.js?
Ich konsultiere die Antwort, und ich habe versucht, das Problem zu lösen, als Ergebnis daraus verstand ich die Javascript Namespacing pattren.
A namespacing pattern to avoid polluting the global namespace.
More details on this namespacing pattern
How do I declare a namespace in JavaScript?
Ich leide an Problem, dass die globale Variable erfolgreich jedoch erstellt wird, weiß ich die erzeugte Variable nicht handhaben.
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;
}
Bitte sehen Sie das Bild unten
Ich denke, die Generation wurde richtig gemacht.
Aber ich verstehe nicht, warum ich diesen Fehler bekomme.
habe ich versucht, auf den ersten
1. Sammlung mit new function
nicht erzeugen Sie (als Strom meine Quelle)
2. Variable videoList
als Objekt erstellen.
3. Speichert Collection
in einer Variablen.
4. Verwenden Sie die Funktion collection.create.
Zum Beispiel list.create({id:'dasdsad'});
jedoch dieser Versuch ergab schließlich das gleiche Ergebnis.
Wie kann ich sie lösen?
enthalten Sie die * collection.js * vor dem * model.js * -Skript? Halte einen Haltepunkt in der * collection.js * und stelle sicher, dass 'app.Model' definiert ist – mikeapr4