vendredi 6 février 2015

How do I set default values for all data types in Ember Data?

I have a model that does not provide default values for any of its properties. I have this model bound to the template and I would like to save the model on transition only if it's dirty. However, simply entering a text field then leaving the text field will cause the record to become dirty, because it previously was null/undefined and now it's an empty string. I was wondering if there is a simple work-around for this?


Model:



App.MyModel = DS.Model.extend({
myProperty: DS.attr('string')
});


Route:



App.MyModelRoute = Ember.Route.extend({
actions: {
willTransition: function(transition) {
var modelIsDirty = this.get('currentModel.isDirty');

if (modelIsDirty) {
console.log('dirty model!');
}
else {
console.log('not dirty');
}
}
}
});


Template:



{{input type="text" value=model.myProperty}}


If I simply focus/blur the input, this will cause the record to become dirty. Is there a way to set the default value for any string property to be '' instead of null or undefined? I think that may fix the problem.


I am aware of the defaultValue property, but I have 15 models and some have 40+ properties each. I would like to avoid setting a defaultValue for every single string.





Aucun commentaire:

Enregistrer un commentaire