Created
March 27, 2012 21:09
-
-
Save cyril-sf/2220260 to your computer and use it in GitHub Desktop.
Ember-data.js with support for functions in DS.attr defaulValue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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