Skip to content

Instantly share code, notes, and snippets.

@cyril-sf
Created March 27, 2012 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyril-sf/2220260 to your computer and use it in GitHub Desktop.
Save cyril-sf/2220260 to your computer and use it in GitHub Desktop.
Ember-data.js with support for functions in DS.attr defaulValue
DS.attr = function(type, options) {
var transform = DS.attr.transforms[type];
ember_assert("Could not find model attribute of type " + type, !!transform);
var transformFrom = transform.from;
var transformTo = transform.to;
options = options || {};
var meta = { type: type, isAttribute: true, options: options };
return Ember.computed(function(key, value) {
var data;
key = options.key || key;
if (arguments.length === 2) {
value = transformTo(value);
this.setProperty(key, value);
} else {
data = get(this, 'data');
value = get(data, key);
if (value === undefined) {
if($.isFunction(options.defaultValue)) {
value = options.defaultValue();
} else {
value = options.defaultValue;
}
}
}
return transformFrom(value);
// `data` is never set directly. However, it may be
// invalidated from the state manager's setData
// event.
}).property('data').cacheable().meta(meta);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment