Made the SuperModel a little bit more clever about handling duplicate loaded models.

This commit is contained in:
Scott Erickson 2014-08-26 16:31:53 -07:00
parent f1bf160755
commit 98752159ca
2 changed files with 22 additions and 1 deletions

View file

@ -119,7 +119,8 @@ module.exports = class SuperModel extends Backbone.Model
for model, i in collection.models for model, i in collection.models
cachedModel = @getModelByURL(model.getURL()) cachedModel = @getModelByURL(model.getURL())
if cachedModel if cachedModel
collection.models[i] = cachedModel clone = $.extend true, {}, model.attributes
cachedModel.set(clone, {silent: true})
else else
@registerModel(model) @registerModel(model)
collection collection

View file

@ -50,3 +50,23 @@ describe 'SuperModel', ->
_.defer -> _.defer ->
expect(triggered).toBe(true) expect(triggered).toBe(true)
done() done()
describe 'collection loading', ->
it 'combines models which are fetched from multiple sources', ->
s = new SuperModel()
c1 = new ComponentsCollection()
c1.url = '/db/level.component?v=1'
s.loadCollection(c1, 'components')
c2 = new ComponentsCollection()
c2.url = '/db/level.component?v=2'
s.loadCollection(c2, 'components')
request = jasmine.Ajax.requests.sendResponses({
'/db/level.component?v=1': [{"_id":"id","name":"Something"}]
'/db/level.component?v=2': [{"_id":"id","description":"This is something"}]
})
expect(s.models['/db/level.component/id'].get('name')).toBe('Something')
expect(s.models['/db/level.component/id'].get('description')).toBe('This is something')