I am sent the assistance by many advisors in stackoverflow, In part of my problem is solved but a few problems are remained.
I consult the answer and I has try to solve the problem as a result from it i understood the 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?
I suffer from problem that global variable is created successfully however, i don't handle the generated variable.
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;
}
I think the generation has been done properly.
But I do not understand why I get this error.
I tried at first
1. Don't generate collection with new function
(as current my source)
2. Create variable videoList
as Object.
3. Stores Collection
in a variable.
4. Use collection.create function.
For example, list.create({id:'dasdsad'});
However, this attempt eventually yielded the same result.
How can i solve them?
What's the purpose of the namespacing pattern?
You are misusing the namespace pattern. The goal in this case is to namespace all your custom Backbone classes constructor into the
app
object.In order to keep everything clearly separated, put all your collections constructor into the
app.Collection
object, models constructor withinapp.Model
, etc.If you inspect the
app
namespace after all the classes are created, it should look like the following:The
app
namespace shouldn't1 contain instances, mainly constructors.Do not overwrite the constructors references:
Create an instance when you need one only, and keep it in the scope it's needed.
Instances and constructors
To really understand the previous point, you need to understand the differences between a constructor and an instance. The concept is applicable to other OOP languages, but I'll keep the description within the JavaScript language specifics.
A constructor is just a function. From the MDN doc on
Object.prototype.constructor
:As seen in the previous example, to create an instance, use the
new
operator. What is the 'new' keyword in JavaScript?Creating custom constructors lets you define custom types (classes). There are multiple ways to create more complex custom types in JavaScript but that's another discussion. For more details, see How to “properly” create a custom object in JavaScript?
Fortunately, Backbone provides an easy (opinionated) way to define new types, the
extend
function, which is available on all the Backbone's base types.Note that
myVariable = MyConstructor
will just pass a reference of the constructor tomyVariable
, it won't create a new instance. You could still use the variable as the constructor though.Ordering and dependencies
If you look at your code, you'll notice that the
app.Collection.VideoList
class uses theapp.Model.Video
as the value for themodel
property.This means that
app.Collection.VideoList
depends on the availability of theapp.Model.Video
class. So the collection file should be inserted into the document after the model file.Like in the other answer of mine you linked:
1. The
app
namespace object could contain an instance of an object if you want to share it between all your app, like a singleton, or a namespaced global, or a service.