Check also associations for incomplete records

This commit is contained in:
Piotr Sarnacki 2012-10-27 04:45:57 +02:00
parent a8e85dc714
commit 6b40f127a1
6 changed files with 133 additions and 7 deletions

View File

@ -8,6 +8,7 @@
get: (name) ->
if @constructor.isAttribute(name) && @get('incomplete') && !@isAttributeLoaded(name)
console.log 'Loading rest of', @constructor, @get('clientId'), 'for key: ', name
@loadTheRest()
@_super.apply this, arguments
@ -23,9 +24,14 @@
this
isAttributeLoaded: (name) ->
key = null
if meta = Ember.get(this.constructor, 'attributes').get(name)
name = meta.key(this.constructor)
@get('store').isDataLoadedFor(this.constructor, @get('clientId'), name)
key = meta.key(this.constructor)
else if meta = Ember.get(this.constructor, 'associationsByName').get(name)
key = meta.options.key || @get('namingConvention').foreignKey(name)
if key
@get('store').isDataLoadedFor(this.constructor, @get('clientId'), key)
isComplete: (->
if @get 'incomplete'
@ -82,4 +88,5 @@
Travis.app.store.adapter.pluralize(@singularName())
isAttribute: (name) ->
Ember.get(this, 'attributes').has(name)
Ember.get(this, 'attributes').has(name) ||
Ember.get(this, 'associationsByName').has(name)

View File

@ -3,6 +3,12 @@ Travis.Foo = Travis.Model.extend
description: DS.attr('string')
lastName: DS.attr('string')
bar: DS.belongsTo('Travis.Bar')
niceBar: DS.belongsTo('Travis.Bar')
veryNiceBar: DS.belongsTo('Travis.Bar', key: 'very_nice_bar_indeed_id')
Travis.Bar = Travis.Model.extend()
record = null
store = null
@ -18,6 +24,52 @@ describe 'Travis.Model', ->
afterEach ->
store.destroy()
describe 'with incomplete record with loaded associations', ->
beforeEach ->
attrs = {
id: 1
bar_id: 2
nice_bar_id: 3
very_nice_bar_indeed_id: 4
}
record = store.loadIncomplete(Travis.Foo, attrs)
store.load(Travis.Bar, id: 2)
store.load(Travis.Bar, id: 3)
store.load(Travis.Bar, id: 4)
it 'does not load record on association access', ->
expect( record.get('bar.id') ).toEqual 2
expect( record.get('niceBar.id') ).toEqual 3
expect( record.get('veryNiceBar.id') ).toEqual 4
waits 50
runs ->
expect( record.get('complete') ).toBeFalsy()
describe 'with incomplete record without loaded associations', ->
beforeEach ->
attrs = {
id: 1
}
record = store.loadIncomplete(Travis.Foo, attrs)
it 'loads record based on regular association key', ->
record.get('bar')
waits 50
runs ->
expect( record.get('complete') ).toBeTruthy()
it 'loads record based on camel case association key', ->
record.get('niceBar')
waits 50
runs ->
expect( record.get('complete') ).toBeTruthy()
it 'loads record based on ssociation with explicit key', ->
record.get('veryNiceBar')
waits 50
runs ->
expect( record.get('complete') ).toBeTruthy()
describe 'with incomplete record', ->
beforeEach ->
attrs = {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -9011,9 +9011,16 @@ return sinon;}.call(typeof window != 'undefined' && window || {}));
Travis.Foo = Travis.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
lastName: DS.attr('string')
lastName: DS.attr('string'),
bar: DS.belongsTo('Travis.Bar'),
niceBar: DS.belongsTo('Travis.Bar'),
veryNiceBar: DS.belongsTo('Travis.Bar', {
key: 'very_nice_bar_indeed_id'
})
});
Travis.Bar = Travis.Model.extend();
record = null;
store = null;
@ -9037,6 +9044,66 @@ return sinon;}.call(typeof window != 'undefined' && window || {}));
afterEach(function() {
return store.destroy();
});
describe('with incomplete record with loaded associations', function() {
beforeEach(function() {
var attrs;
attrs = {
id: 1,
bar_id: 2,
nice_bar_id: 3,
very_nice_bar_indeed_id: 4
};
record = store.loadIncomplete(Travis.Foo, attrs);
store.load(Travis.Bar, {
id: 2
});
store.load(Travis.Bar, {
id: 3
});
return store.load(Travis.Bar, {
id: 4
});
});
return it('does not load record on association access', function() {
expect(record.get('bar.id')).toEqual(2);
expect(record.get('niceBar.id')).toEqual(3);
expect(record.get('veryNiceBar.id')).toEqual(4);
waits(50);
return runs(function() {
return expect(record.get('complete')).toBeFalsy();
});
});
});
describe('with incomplete record without loaded associations', function() {
beforeEach(function() {
var attrs;
attrs = {
id: 1
};
return record = store.loadIncomplete(Travis.Foo, attrs);
});
it('loads record based on regular association key', function() {
record.get('bar');
waits(50);
return runs(function() {
return expect(record.get('complete')).toBeTruthy();
});
});
it('loads record based on camel case association key', function() {
record.get('niceBar');
waits(50);
return runs(function() {
return expect(record.get('complete')).toBeTruthy();
});
});
return it('loads record based on ssociation with explicit key', function() {
record.get('veryNiceBar');
waits(50);
return runs(function() {
return expect(record.get('complete')).toBeTruthy();
});
});
});
describe('with incomplete record', function() {
beforeEach(function() {
var attrs;

View File

@ -1 +1 @@
bce1e34d
6eb17b9d