Backbone.Mutators
Backbone.Mutators copied to clipboard
Can't override getter - Maximum call stack size exceeded
Test = Backbone.Model.extend
defaults:
name: ''
mutators:
name: ->
@get('name') + ' mutated'
test = new Test
#> Maximum call stack size exceeded
Should work, unless Coffescript does something weird in terms of scoping... Try in vanilla JavaScript & please report if the issue still persists.
I'm having the same issue using vanilla JavaScript
Same issue for me
I think I had the same problem but solved it like this :
var User = Backbone.Model.extend({
"mutators": {
/**
* Profile mutator, creates a profile model from the existing data
*
* @return {Profile} Profile model instance
*/
"profile": function() {
// /!\ Access attributes object instead of calling this.get("profile")
var profile = this.attributes.profile;
if (_.isObject(profile) && !profile.cid) {
var newProfile = new Profile();
// Check if the existing data only consists of an id or not
if (Object.keys(profile).length === 1 && profile.id) {
// Only set the id
newProfile.set("id", profile.id);
} else {
// Create profile model instance from existing data
newProfile.set(profile);
}
profile = newProfile;
this.set("profile", newProfile);
}
return profile;
}
}
});
I used mutators to implement nested model instances, but when using this.get("profile") inside the profile mutator I had the same error. Infinit method loop I guessed.
@asciidisco I believe the issue is still present in vanilla JavaScript. I've put together a simple JSFiddle here to demonstrate. The code is a straight copy-and-paste from the Backbone.Mutators docs.
It throws an Uncaught RangeError: Maximum call stack size exceeded because it just keeps calling the mutated getter recursively forever.
Maybe we could pass in the original Backbone getter as an argument to use within the mutated getter? For example, similar to how it's currently done with setters.
For now, an easy workaround is to just use a different name for your mutated getter. But it would be nice to be able to use the same name as an existing model property.
+1 same issue
Same here
@RasCarlito 's solution works for me +1
Yup, same here. Still broken. @RasCarlito 's solution does work.