Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't override getter - Maximum call stack size exceeded #30

Open
jurgens opened this issue Aug 16, 2014 · 9 comments
Open

Can't override getter - Maximum call stack size exceeded #30

jurgens opened this issue Aug 16, 2014 · 9 comments
Assignees
Labels

Comments

@jurgens
Copy link

jurgens commented Aug 16, 2014

Test = Backbone.Model.extend

  defaults:
    name: ''

  mutators:
    name: ->
      @get('name') + ' mutated'

test = new Test
#> Maximum call stack size exceeded 
@asciidisco
Copy link
Owner

Should work, unless Coffescript does something weird in terms of scoping...
Try in vanilla JavaScript & please report if the issue still persists.

@asciidisco asciidisco added the bug label Aug 18, 2014
@asciidisco asciidisco self-assigned this Aug 18, 2014
@madtrick
Copy link

madtrick commented Oct 3, 2014

I'm having the same issue using vanilla JavaScript

@RomanKapitonov
Copy link

Same issue for me

@ghost
Copy link

ghost commented Dec 22, 2014

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.

@danielmcormond
Copy link

@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.

@mmrcdx
Copy link

mmrcdx commented Feb 24, 2015

+1 same issue

@HiroAgustin
Copy link

Same here

@passabilities
Copy link

@RasCarlito 's solution works for me +1

@cnobile2012
Copy link

Yup, same here. Still broken. @RasCarlito 's solution does work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants