Model Declaration: false vs DS.attr('boolean', {defaultValue: false})


#1

I am 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!!!

So I guess my question is this: is 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.


#2

One’s just a plain boolean, the other one is actually a DS.attr(), so bindings will be setup correctly. That’s why the latter dirties the model.


#3

Using a boolean instead of DS.attr(‘boolean’) in previous versions of ember.data dirtied the model. I’ve recently upgraded to ember.data 1.0.0-beta.8 and I’ve noticed this behavior change.

Trying to figure out if this is by design or a bug.


#4

oh! Yeah, that makes sense. How old is the Ember Data you’re using? You may also want to look into the .find() changes if you’re coming from a really old version.


#5

The 2nd version is better practice, though.


#6

Design, of course.

Not to serialize attributes different from DS.attr() allows you to have local properties on your models that does not affect the communication with the server.

Say, a computed property calculated over a bunch of other attributes that make sense on the client side, but there is no need for the server to store or bother about.


#7

Makes sense. Just wanted to verify the behavior. Thanks!