I have this model and a collection for it:
app.models.Result= Backbone.Model.extend({
idAttribute: 'uid',
initialize: function () {
this.set('name', this.get('name').replace(' ', '').substring(0, 20));
},
});
app.models.ResultList = Backbone.Collection.extend({
model: app.models.Result,
url: 'http://localhost/stuff',
parse: function (response, options) {
this.add(response.data);
console.log(this);
return response.data;
},
});
Then I use it in my view's initialize()
function like this:
var that = this;
this.collection = new app.models.SearchList();
this.collection.fetch({
dataType: 'jsonp',
success: function () {
console.log(that.collection);
that.collection.each(function (resultModel) {
console.log('ok');
});
}
});
With this code I almost get what I want, but when I look at the second log()
I can see that the name
property is the same as it was in the fetched JSON.
My guess is because I simply returned the response.data
, so I modified it to this
. In this case I get an error:
Uncaught TypeError: Cannot call method 'replace' of undefined
So I commented out that line. Now I can see that all the models are gone. The array has only one model in it but that is not one from the JSON.
What happend with my beloved models? Where are they? Should I save them somehow? At the first log this
pointed to the correct full collection, so why doesn't returning this
gives me the same correct collection.