From 89efdec071ea0700139e300f1c0b79bb16143acb Mon Sep 17 00:00:00 2001 From: Andrew Ross Date: Tue, 7 Jul 2015 22:24:10 -0400 Subject: [PATCH] extract model.pushObjects to a method to allow overrides in the case when more specialized munging/manipulation might be required --- README.md | 13 +++++++ addon/mixins/route.js | 20 ++++++++-- tests/unit/mixins/route-test.js | 67 +++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d433de8b..f35cc33d 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,19 @@ and ember-infinity will be set up to parse the total number of pages from a JSON } ``` +You may override `updateInfinityModel` to customize how the route's `model` should be updated with new objects. You may also invoke this method directly to manually push new objects into the model: + +```js +actions: { + pushHughIntoInfinityModel() [ + var updatedInfinityModel = this.updateInfinityModel([ + { id: 1, name: "Hugh Francis" } + ]); + console.log(updatedInfinityModel); + } +} +``` + ### infinityModel You can also provide additional parameters to `infinityModel` that diff --git a/addon/mixins/route.js b/addon/mixins/route.js index ca65f84f..76f83501 100644 --- a/addon/mixins/route.js +++ b/addon/mixins/route.js @@ -173,7 +173,6 @@ export default Ember.Mixin.create({ var nextPage = this.get('_currentPage') + 1; var perPage = this.get('_perPage'); var totalPages = this.get('_totalPages'); - var model = this.get(this.get('_modelPath')); var modelName = this.get('_infinityModelName'); if (!this.get('_loadingMore') && this.get('_canLoadMore')) { @@ -187,14 +186,14 @@ export default Ember.Mixin.create({ var promise = this.store.find(modelName, params); promise.then( - infinityModel => { - model.pushObjects(infinityModel.get('content')); + newObjects => { + this.updateInfinityModel(newObjects); this.set('_loadingMore', false); this.set('_currentPage', nextPage); Ember.run.scheduleOnce('afterRender', this, 'infinityModelUpdated', { lastPageLoaded: nextPage, totalPages: totalPages, - newObjects: infinityModel + newObjects: newObjects }); if (!this.get('_canLoadMore')) { this.set(this.get('_modelPath') + '.reachedInfinity', true); @@ -217,6 +216,19 @@ export default Ember.Mixin.create({ return false; }, + /** + Update the infinity model with new objects + + @method updateInfinityModel + @param {Ember.Enumerable} newObjects The new objects to add to the model + @return {Ember.Array} returns the updated infinity model + */ + updateInfinityModel(newObjects) { + var infinityModel = this.get(this.get('_modelPath')); + + return infinityModel.pushObjects(newObjects.get('content')); + }, + actions: { infinityLoad() { this._infinityLoad(); diff --git a/tests/unit/mixins/route-test.js b/tests/unit/mixins/route-test.js index 0f8570fe..304087ed 100644 --- a/tests/unit/mixins/route-test.js +++ b/tests/unit/mixins/route-test.js @@ -308,3 +308,70 @@ test('it uses overridden params when loading more data', assert => { assert.ok(model.get('reachedInfinity'), 'Should reach infinity'); }); + +test('it allows overrides/manual invocations of updateInfinityModel', assert => { + var RouteObject = Ember.Route.extend(RouteMixin, { + model() { + return this.infinityModel('item', {perPage: 1}); + }, + updateInfinityModel(newObjects) { + return this._super(newObjects.setEach('author', 'F. Scott Fitzgerald')); + } + }); + var route = RouteObject.create(); + + var items = [ + { id: 1, title: 'The Great Gatsby' }, + { id: 2, title: 'The Last Tycoon' } + ]; + + var dummyStore = { + find(modelType, findQuery) { + var item = items[findQuery.page-1]; + return new Ember.RSVP.Promise(resolve => { + Ember.run(this, resolve, Ember.ArrayProxy.create({ + content: Ember.A([item]), + meta: { total_pages: 2 } + })); + }); + } + }; + + route.store = dummyStore; + + var model; + Ember.run(() => { + route.model().then(result => { + model = result; + }); + }); + + var dummyController = Ember.Object.create({ + model + }); + route.set('controller', dummyController); + + assert.equal(route.get('_canLoadMore'), true); + assert.equal(model.get('content.length'), 1); + + Ember.run(() => { + route._infinityLoad(); + }); + + assert.equal(route.get('_canLoadMore'), false); + assert.equal(model.get('content.length'), 2); + assert.equal(model.get('content.lastObject.author'), 'F. Scott Fitzgerald', 'overrides to updateInfinityModel should take effect'); + + var newObjects = Ember.ArrayProxy.create({ + content: Ember.A([ + { id: 3, title: 'Tender Is the Night' } + ]) + }); + + Ember.run(() => { + route.updateInfinityModel(newObjects); + }); + + assert.equal(model.get('content.length'), 3); + assert.equal(model.get('content.lastObject.title'), 'Tender Is the Night', 'updateInfinityModel can be invoked manually'); +});