Reputation: 11725
I'm trying to make loading my model easier. If the model isn't already fetched, it simply uses the id
field and fetches itself. I then use _.extend(this, fetchedModel)
to update this
. The problem is now I'm running into cyclic activities when I try to set this fetched and extended model as a property in another model — othermodel.set("fetchedModel", fetchedModel)
.
Is there another way to fetch my model and simply overwrite the entire current instance of this
with what was just fetched?
Here's my code for context (I'm using Parse, as well):
load : function(success) {
debug("Loading workout...");
// if it's not grabbed, grab it
if(!this.createdAt) {
var q = new Parse.Query(Workout);
q.include("user");
return q.get(this.id, {
success : function(workout) {
_.extend(this, workout);
this.load(success);
}.bind(this)
});
}
Thanks.
Upvotes: 0
Views: 160
Reputation: 35920
The infinite recursion is caused by the success callback in the method load
calling this.load
, i.e. itself.
To reset the attributes of the model, _.extend(this, workout)
is not the correct way to go. That will copy the properties of workout
into the model object itself. What you want to do is to update the model's internal attributes
object using Model#set
:
this.set(workout);
If you want to ensure that the new properties not only override old properties, but also clear out any previous properties not present in the new object, also call Model#clear
:
this.clear();
this.set(workout);
As an aside, typically to update Backbone model, you would simply call Model#fetch
, like so:
this.fetch({
success: function() {
//model has been updated...
}
});
Alas, I don't know what's the best practice with parse.com. The documentation for Parse.Object
seems to mention the fetch method.
Upvotes: 3