Skip to content

Commit

Permalink
references
Browse files Browse the repository at this point in the history
  • Loading branch information
igorT committed May 31, 2015
1 parent 1b1bd43 commit 0682055
Show file tree
Hide file tree
Showing 28 changed files with 954 additions and 628 deletions.
13 changes: 9 additions & 4 deletions packages/ember-data/lib/system/many-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PromiseArray } from "ember-data/system/promise-proxies";
var get = Ember.get;
var set = Ember.set;
var filter = Ember.ArrayPolyfills.filter;
var map = Ember.EnumerableUtils.map;

/**
A `ManyArray` is a `MutableArray` that represents the contents of a has-many
Expand Down Expand Up @@ -57,19 +58,23 @@ export default Ember.Object.extend(Ember.MutableArray, Ember.Evented, {
length: 0,

objectAt: function(index) {
return this.currentState[index];
//Ember observers such as 'firstObject', 'lastObject' might do out of bounds accesses
if (!this.currentState[index]) {
return undefined;
}
return this.currentState[index].getRecord();
},

flushCanonical: function() {
//TODO make this smarter, currently its plenty stupid
var toSet = filter.call(this.canonicalState, function(record) {
return !record.get('isDeleted');
return !record.isDeleted();
});

//a hack for not removing new records
//TODO remove once we have proper diffing
var newRecords = this.currentState.filter(function(record) {
return record.get('isNew');
return record.isNew();
});
toSet = toSet.concat(newRecords);
var oldLength = this.length;
Expand Down Expand Up @@ -143,7 +148,7 @@ export default Ember.Object.extend(Ember.MutableArray, Ember.Evented, {
this.get('relationship').removeRecords(records);
}
if (objects) {
this.get('relationship').addRecords(objects, idx);
this.get('relationship').addRecords(map(objects, function(obj) { return obj.reference; }), idx);
}
},
/**
Expand Down
14 changes: 8 additions & 6 deletions packages/ember-data/lib/system/model/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,25 +300,27 @@ export default function attr(type, options) {

return computedPolyfill({
get: function(key) {
if (hasValue(this, key)) {
return getValue(this, key);
var reference = this.reference;
if (hasValue(reference, key)) {
return getValue(reference, 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);
var reference = this.reference;
var oldValue = getValue(reference, key);

if (value !== oldValue) {
// Add the new value to the changed attributes hash; it will get deleted by
// the 'didSetProperty' handler if it is no different from the original value
this._attributes[key] = value;
reference._attributes[key] = value;

this.send('didSetProperty', {
this.reference.send('didSetProperty', {
name: key,
oldValue: oldValue,
originalValue: this._data[key],
originalValue: reference._data[key],
value: value
});
}
Expand Down
Loading

0 comments on commit 0682055

Please sign in to comment.