Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazily materialize DS.Models for app code, use InternalModel inside ED otherwise #3094

Merged
merged 1 commit into from
Jun 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/ember-data/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import "ember-data/ext/date";

import normalizeModelName from "ember-data/system/normalize-model-name";

import InternalModel from "ember-data/system/model/internal-model";

import {
PromiseArray,
PromiseObject,
Expand Down Expand Up @@ -85,6 +87,7 @@ DS.RootState = RootState;
DS.attr = attr;
DS.Errors = Errors;

DS.InternalModel = InternalModel;
DS.Snapshot = Snapshot;

DS.Adapter = Adapter;
Expand Down
17 changes: 11 additions & 6 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');
var toSet = filter.call(this.canonicalState, function(internalModel) {
return !internalModel.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');
var newRecords = this.currentState.filter(function(internalModel) {
return internalModel.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._internalModel; }), 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 internalModel = this._internalModel;
if (hasValue(internalModel, key)) {
return getValue(internalModel, 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 internalModel = this._internalModel;
var oldValue = getValue(internalModel, 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;
internalModel._attributes[key] = value;

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