I think you just have a typo. model: Todo
should be model: TodoModel
Setting a model type to a collection with external url in backbone [closed]
-
04-12-2021 - |
Question
I am creating a collection that has an external url something like this:
var todoCollection = Backbone.Collection.extend({
model: Todo
url: function() {
return "http:externalurl.com";
},
parse: function(dat) {
return dat.obj.data;
}
});
return new todosCollection;
and my model looks like this:
var TodoModel = Backbone.Model.extend({
initialize: function() {}
});
return TodoModel;
Now here in my view I use the collection in this way:
$.each(this.collection.models,function(i,model){
console.log(model);
})
The problem is, my models are not getting set as the TodoModel type. They are simple Object types. Can someone help me in letting me know where I am going wrong here?
Thank you.
Solution
OTHER TIPS
I don't think you are correct. The console.log
may say it is an object but it is almost certainly an instance of your Model. Also, if the code in your example is your real code then it is broken because you are using the each
function with backwards parameters and you are using Todo
where you should be using TodoModel
.
this.collection.each(function (model) {
console.log(model instanceof TodoModel);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow