Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I define a default value for ember-data model attributes?

Tags:

When I define an attribute for a model in ember-data, can I specify a default value?

The ember-data model definition suggests attributes are defined like this:

attributeName: DS.attr('number') 

...with an optional second argument as an options hash. I've looked at the code but my code-reading skills aren't up to figuring out what attributes I can put in the options hash. Is it possible to do something like this?

attributeName: DS.attr('number', { default: 0 }) 

or

attributeName: DS.attr('boolean', { default: false }) 

?

like image 792
pjmorse Avatar asked Jun 04 '12 20:06

pjmorse


2 Answers

You were close, it's defaultValue, see attributes.js#L63-65.

like image 176
pangratz Avatar answered Oct 15 '22 23:10

pangratz


defaultValue doesn't work if the property is null. It only works for 'undefined' attributes. See this

like image 29
iOS dev Avatar answered Oct 15 '22 23:10

iOS dev