Skip to content

Commit

Permalink
Use new getter/setter for computed if available
Browse files Browse the repository at this point in the history
  • Loading branch information
wecc committed Apr 1, 2015
1 parent 339b79a commit c53815f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
21 changes: 11 additions & 10 deletions packages/ember-data/lib/system/model/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
Map
} from "ember-data/system/map";

import computedPolyfill from "ember-data/utils/computed-polyfill";

/**
@module ember-data
*/
Expand Down Expand Up @@ -296,8 +298,15 @@ export default function attr(type, options) {
options: options
};

return Ember.computed(function(key, value) {
if (arguments.length > 1) {
return computedPolyfill({
get: function(key) {
if (hasValue(this, key)) {
return getValue(this, key);
} else {
return getDefaultValue(this, options, key);
}
},
set: function(key, value) {
Ember.assert("You may not set `id` as an attribute on your model. Please remove any lines that look like: `id: DS.attr('<type>')` from " + this.constructor.toString(), key !== 'id');
var oldValue = getValue(this, key);

Expand All @@ -315,14 +324,6 @@ export default function attr(type, options) {
}

return value;
} else if (hasValue(this, key)) {
return getValue(this, key);
} else {
return getDefaultValue(this, options, key);
}

// `data` is never set directly. However, it may be
// invalidated from the state manager's setData
// event.
}).meta(meta);
}
15 changes: 10 additions & 5 deletions packages/ember-data/lib/system/relationships/belongs-to.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Model from 'ember-data/system/model';

import computedPolyfill from "ember-data/utils/computed-polyfill";

/**
`DS.belongsTo` is used to define One-To-One and One-To-Many
relationships on a [DS.Model](/api/data/classes/DS.Model.html).
Expand Down Expand Up @@ -76,19 +78,22 @@ function belongsTo(type, options) {
key: null
};

return Ember.computed(function(key, value) {
if (arguments.length>1) {
if ( value === undefined ) {
return computedPolyfill({
get: function(key) {
return this._relationships[key].getRecord();
},
set: function(key, value) {
if (value === undefined) {
value = null;
}
if (value && value.then) {
this._relationships[key].setRecordPromise(value);
} else {
this._relationships[key].setRecord(value);
}
}

return this._relationships[key].getRecord();
return this._relationships[key].getRecord();
}
}).meta(meta);
}

Expand Down
35 changes: 35 additions & 0 deletions packages/ember-data/lib/utils/computed-polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import supportsComputedGetterSetter from './supports-computed-getter-setter';

var computed = Ember.computed;

export default function() {
var polyfillArguments = [];
var config = arguments[arguments.length - 1];

if (typeof config === 'function' || supportsComputedGetterSetter) {
return computed.apply(null, arguments);
}

for (var i = 0, l = arguments.length - 1; i < l; i++) {
polyfillArguments.push(arguments[i]);
}

var func;
if (config.set) {
func = function(key, value) {
if (arguments.length > 1) {
return config.set.call(this, key, value);
} else {
return config.get.call(this, key);
}
};
} else {
func = function(key) {
return config.get.call(this, key);
};
}

polyfillArguments.push(func);

return computed.apply(null, polyfillArguments);
}
13 changes: 13 additions & 0 deletions packages/ember-data/lib/utils/supports-computed-getter-setter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var supportsComputedGetterSetter;

try {
Ember.computed({
get: function() { },
set: function() { }
});
supportsComputedGetterSetter = true;
} catch(e) {
supportsComputedGetterSetter = false;
}

export default supportsComputedGetterSetter;

0 comments on commit c53815f

Please sign in to comment.