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

[WIP] Partially loaded recrods #86

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions packages/ember-model/lib/attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ Ember.attr = function(type) {
dataValue = data && get(data, dataKey),
beingCreated = meta(this).proto === this;

if(this.get('isPartiallyLoaded') && this.isAttributeMissing(key)) {
this.missingAttributeAccessed(key);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing space on indent

}

if (arguments.length === 2) {
if (beingCreated && !data) {
data = {};
Expand Down
21 changes: 19 additions & 2 deletions packages/ember-model/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,32 @@ Ember.Model = Ember.Object.extend(Ember.Evented, Ember.DeferredMixin, {
},

load: function(id, hash) {
var data = {};
var data = {}, isPartiallyLoaded, attributes;
data[get(this.constructor, 'primaryKey')] = id;
set(this, 'data', Ember.merge(data, hash));
data = Ember.merge(data, hash);
this._loadedAttributes = Ember.A(Ember.keys(data).map(function(key) { return this.dataKey(key); }, this));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather you just loop here, it'll be significantly faster.


attributes = get(this, 'attributes');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just use this.attributes here

if(!attributes || attributes.length === 0) {
// TODO: fix this when implementing support for relationships
isPartiallyLoaded = false;
} else {
isPartiallyLoaded = Ember.EnumerableUtils.intersection(this._loadedAttributes, attributes).length !== attributes.length;
}
set(this, 'isPartiallyLoaded', isPartiallyLoaded);
set(this, 'data', data);
set(this, 'isLoaded', true);
set(this, 'isNew', false);
this.trigger('didLoad');
this.resolve(this);
},

isAttributeMissing: function(key) {
return this.get('isPartiallyLoaded') && this._loadedAttributes && !this._loadedAttributes.contains(key);
},

missingAttributeAccessed: Ember.K,

didDefineProperty: function(proto, key, value) {
if (value instanceof Ember.Descriptor) {
var meta = value.meta();
Expand Down
50 changes: 50 additions & 0 deletions packages/ember-model/tests/partially_loaded_model_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var Model;

module("Partially loaded records", {
setup: function() {
Model = Ember.Model.extend({
id: Ember.attr(),
name: Ember.attr()
});

Model.adapter = Ember.FixtureAdapter.create();
}
});

test("missingAttributeAccessed is called if the attribute is not loaded", function() {
expect(1);

var key, record = Model.create({ isLoaded: false });
Ember.run(function() {
record.load(1, {});
});

record.missingAttributeAccessed = function(_key) {
key = _key;
};

record.get('name');

equal(key, 'name', 'missingAttributeAccessed should be called with attribute name "key".');
});

test("missingAttributeAccessed is not called if the attribute is loaded", function() {
expect(0);

var key, record = Model.create({ isLoaded: false });
Ember.run(function() {
record.load(1, { name: 'Erik' });
});

record.missingAttributeAccessed = function(_key) {
key = _key;
};

record.get('name');

record.missingAttributeAccessed = function(_key) {
equal(true, false, "missingAttributeAccessed should not be called");
};

record.get('name');
});