Skip to content

Instantly share code, notes, and snippets.

@cyril-sf
Created March 27, 2012 21:09
Show Gist options
  • 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.

Revisions

  1. cyril-sf revised this gist Mar 27, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@



    DS.attr = function(type, options) {
    var transform = DS.attr.transforms[type];
    ember_assert("Could not find model attribute of type " + type, !!transform);
  2. cyril-sf created this gist Mar 27, 2012.
    42 changes: 42 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@



    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);
    };