Matt Blair

Matt Blair

I read that you learn more from a poor example than from a correct one. I don't believe this but that means my site will be a success.

Ember.Data Model Issues

Ember.Data Model Issues

1-Minute Read

I was working with some older Ember.Data code, and I came across a model like this:

App.MyFancyModel = DS.Model.extend({
  isSelected: false,
  isSomethingElse: DS.attr('boolean', {defaultValue: false})
});

I thought this code was a bit strange, and then went and played with it a bit:

aFancyModel.get('isSelected'); //returns false
aFancyModel.set('isSelected', true);
aFancyModel.get('isSelected'); //returns true
aFancyModel.get('isSomethingElse'); //returns false
aFancyModel.set('isSomethingElse', true);
aFancyModel.get('isSomethingElse'); //returns true

I got identical behavior from the two properties. Then, I tried this:

aFancyModel.set('isSelected', true);
aFancyModel.get('isDirty'); //RETURNS FALSE
aFancyModel.set('isSomethingElse', true);
aFancyModel.get('isDirty'); //RETURNS TRUE!!!

My question was this: was this the expected behavior? I can’t find any documentation on setting boolean values directly on the model like this anywhere in the Ember.Data docs.

After asking around on discuss.emberjs.com, I got an answer.

This behavior is by design. Fields designated with just a variable (e.g. isSelected) are local attributes. You can use them just like an other attribute.

The difference with fields declared this way is they won’t dirty the model and they aren’t sent across the wire on save or update.

Recent Posts

Categories

About

This theme was developed for Hugo.