From 98752159ca5928b4338348d5902a917d569a4fd4 Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Tue, 26 Aug 2014 16:31:53 -0700 Subject: [PATCH] Made the SuperModel a little bit more clever about handling duplicate loaded models. --- app/models/SuperModel.coffee | 3 ++- test/app/models/SuperModel.spec.coffee | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/models/SuperModel.coffee b/app/models/SuperModel.coffee index d16cd725f..809f66383 100644 --- a/app/models/SuperModel.coffee +++ b/app/models/SuperModel.coffee @@ -119,7 +119,8 @@ module.exports = class SuperModel extends Backbone.Model for model, i in collection.models cachedModel = @getModelByURL(model.getURL()) if cachedModel - collection.models[i] = cachedModel + clone = $.extend true, {}, model.attributes + cachedModel.set(clone, {silent: true}) else @registerModel(model) collection diff --git a/test/app/models/SuperModel.spec.coffee b/test/app/models/SuperModel.spec.coffee index 1e64d8d14..12219a584 100644 --- a/test/app/models/SuperModel.spec.coffee +++ b/test/app/models/SuperModel.spec.coffee @@ -50,3 +50,23 @@ describe 'SuperModel', -> _.defer -> expect(triggered).toBe(true) 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')