This.set is not a function


#1

This is happening to me regularly. I have an object (model, controller, whatever), I can read it’s properties with this.get(‘foo’), but somehow I can’t use this.set(‘foo’, ‘bar’). In the console, it says this.set is not a function.

Here’s an example. I create a new record and call a methode immediately. If I’m right, createRecords returns a model records, not a promise.

var newModel = this.store.createRecord('serving', {});
newModel.setFoo();

So…

setFoo: function() {
  this.set('foo', 'bar');
}

… throws the this.set is not a function error. Why?


#2

Shouldn’t it be newModel.setFoo()?


#3

Ofcourse, just a copy-past failure. But the problem is still the same. setFoo is called, I can use this.get() without problem, but no this.set().


#4

Are you able to reprocude this in a jsbin example? (http://emberjs.jsbin.com)


#5

To answer my own question:

Sometimes, somehow Ember sees an object not as an Ember object, so no set() methode is present. In that case, do it like this:

Ember.set(newModel, 'foo', 'bar');