raytiley
raytiley

Reputation: 684

Emberjs Computed Property Calculated for Unknown Reason

So I have what I think is a simple Ember Object.

App.Playlist = Ember.Model.extend({
  _clips: [],
  clips: function() {
    var self = this;  
    if(this.get('clipIds')) {
      this.get('clipIds').forEach(function(id) {
        self.get('_clips').addObject({});
      }
    }
  }.property('clipIds')
});

The problem is that the clips computed property gets called infinitely until it raises an exception Uncaught RangeError: Maximum call stack size exceeded

Upvotes: 0

Views: 333

Answers (2)

raytiley
raytiley

Reputation: 684

So here is how I ended up fixing this for now. Still not sure why the computed property gets called repeatedly.

App.Playlist = Ember.Model.extend({
  clips: [],
  loadClips: function() {
    var self = this;  
    if(this.get('clipIds')) {
      this.get('clipIds').forEach(function(id) {
        self.get('clips').addObject({});
      }
    }
  }.observes('clipIds.@each')
});

Upvotes: 0

stevekane
stevekane

Reputation: 372

Ray, this should be defined differently. Computed properties are defined like functions and Ember will handle calling your function when it observes a change to whatever dependencies you define.

App.Playlist = Ember.Model.extend({
  myComputed: function () {
    return this.get('clipIds').map(function (id) {
      return Ember.Clip.create({id: id});
    });
  }.property('clipIds.@each'),
});

this code would watch some property called "clipIds" (whatever that is) and would return a list of Ember.Clip objects based on that array of clipIds.

Upvotes: 1

Related Questions