Skip to content

Commit

Permalink
Refactor to align with new ES6 language features
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike North committed May 21, 2015
1 parent 7f10048 commit 18f2145
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 78 deletions.
31 changes: 17 additions & 14 deletions addon/components/infinity-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,44 @@ export default Ember.Component.extend({
developmentMode: false,
scrollable: null,

didInsertElement: function() {
didInsertElement() {
this._super(...arguments);
this._setupScrollable();
this.set('guid', Ember.guidFor(this));
this._bindScroll();
this._checkIfInView();
},

willDestroyElement: function() {
willDestroyElement() {
this._super(...arguments);
this._unbindScroll();
},

_bindScroll: function() {
var _this = this;
this.get("scrollable").on("scroll."+this.get('guid'), function() {
Ember.run.debounce(_this, _this._checkIfInView, _this.get('scrollDebounce'));
_bindScroll() {
this.get("scrollable").on(`scroll.${this.get('guid')}`, () => {
Ember.run.debounce(this, this._checkIfInView, this.get('scrollDebounce'));
});
},

_unbindScroll: function() {
this.get("scrollable").off("scroll."+this.get('guid'));
_unbindScroll() {
this.get("scrollable").off(`scroll.${this.get('guid')}`);
},

_checkIfInView: function() {
_checkIfInView() {
var selfOffset = this.$().offset().top;
var scrollable = this.get("scrollable");
var scrollableBottom = scrollable.height() + scrollable.scrollTop();

var inView = selfOffset < scrollableBottom ? true : false;
var inView = selfOffset < scrollableBottom;

if (inView && !this.get('developmentMode')) {
this.sendAction('loadMoreAction');
}
},

_setupScrollable: function() {
_setupScrollable() {
var scrollable = this.get('scrollable');
if (Ember.$.type(scrollable) === 'string') {
if (Ember.typeOf(scrollable) === 'string') {
var items = Ember.$(scrollable);
if (items.length === 1) {
this.set('scrollable', items.eq(0));
Expand All @@ -64,7 +65,9 @@ export default Ember.Component.extend({
}
},

loadedStatusDidChange: Ember.observer('infinityModel.reachedInfinity', 'destroyOnInfinity', function() {
if (this.get('infinityModel.reachedInfinity') && this.get('destroyOnInfinity')) { this.destroy(); }
loadedStatusDidChange: Ember.observer('infinityModel.reachedInfinity', 'destroyOnInfinity', function () {
if (this.get('infinityModel.reachedInfinity') && this.get('destroyOnInfinity')) {
this.destroy();
}
})
});
49 changes: 29 additions & 20 deletions addon/mixins/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default Ember.Mixin.create({
_canLoadMore: Ember.computed('_totalPages', '_currentPage', function() {
var totalPages = this.get('_totalPages');
var currentPage = this.get('_currentPage');
return totalPages && currentPage ? currentPage < totalPages : false;
return (totalPages && currentPage) ? (currentPage < totalPages) : false;
}),

/**
Expand All @@ -113,8 +113,7 @@ export default Ember.Mixin.create({
@param {Object} options Optional, the perPage and startingPage to load from.
@return {Ember.RSVP.Promise}
*/
infinityModel: function(modelName, options) {
var _this = this;
infinityModel(modelName, options) {

if (Ember.isEmpty(this.store) || Ember.isEmpty(this.store.find)){
throw new Ember.Error("Ember Data store is not available to infinityModel");
Expand Down Expand Up @@ -147,10 +146,14 @@ export default Ember.Mixin.create({
promise.then(
infinityModel => {
var totalPages = infinityModel.get(this.get('totalPagesParam'));
_this.set('_currentPage', startingPage);
_this.set('_totalPages', totalPages);
infinityModel.set('reachedInfinity', !_this.get('_canLoadMore'));
Ember.run.scheduleOnce('afterRender', _this, 'infinityModelUpdated', { lastPageLoaded: startingPage, totalPages: totalPages, newObjects: infinityModel });
this.set('_currentPage', startingPage);
this.set('_totalPages', totalPages);
infinityModel.set('reachedInfinity', !this.get('_canLoadMore'));
Ember.run.scheduleOnce('afterRender', this, 'infinityModelUpdated', {
lastPageLoaded: startingPage,
totalPages: totalPages,
newObjects: infinityModel
});
},
() => {
throw new Ember.Error("Could not fetch Infinity Model. Please check your serverside configuration.");
Expand All @@ -166,8 +169,7 @@ export default Ember.Mixin.create({
@method infinityLoad
@return {Boolean}
*/
_infinityLoad: function() {
var _this = this;
_infinityLoad() {
var nextPage = this.get('_currentPage') + 1;
var perPage = this.get('_perPage');
var totalPages = this.get('_totalPages');
Expand All @@ -179,33 +181,40 @@ export default Ember.Mixin.create({

var params = Ember.merge({ page: nextPage, per_page: perPage }, this.get('_extraParams'));
var promise = this.store.find(modelName, params);

promise.then(
function(infinityModel) {
infinityModel => {
model.pushObjects(infinityModel.get('content'));
_this.set('_loadingMore', false);
_this.set('_currentPage', nextPage);
Ember.run.scheduleOnce('afterRender', _this, 'infinityModelUpdated', { lastPageLoaded: nextPage, totalPages: totalPages, newObjects: infinityModel });
if (!_this.get('_canLoadMore')) {
_this.set(_this.get('_modelPath') + '.reachedInfinity', true);
Ember.run.scheduleOnce('afterRender', _this, 'infinityModelLoaded', { totalPages: totalPages });
this.set('_loadingMore', false);
this.set('_currentPage', nextPage);
Ember.run.scheduleOnce('afterRender', this, 'infinityModelUpdated', {
lastPageLoaded: nextPage,
totalPages: totalPages,
newObjects: infinityModel
});
if (!this.get('_canLoadMore')) {
this.set(this.get('_modelPath') + '.reachedInfinity', true);
Ember.run.scheduleOnce('afterRender', this, 'infinityModelLoaded', {
totalPages: totalPages
});
}
},
function() {
_this.set('_loadingMore', false);
() => {
this.set('_loadingMore', false);
throw new Ember.Error("You must pass a Model Name to infinityModel");
}
);
} else {
if (!this.get('_canLoadMore')) {
this.set(this.get('_modelPath') + '.reachedInfinity', true);
Ember.run.scheduleOnce('afterRender', _this, 'infinityModelLoaded', { totalPages: totalPages });
Ember.run.scheduleOnce('afterRender', this, 'infinityModelLoaded', { totalPages: totalPages });
}
}
return false;
},

actions: {
infinityLoad: function() {
infinityLoad() {
this._infinityLoad();
}
}
Expand Down
14 changes: 7 additions & 7 deletions tests/acceptance/infinity-route-with-meta-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ var posts = [
];

module('Acceptance: Infinity Route', {
setup: function() {
setup() {
App = startApp();
server = new Pretender(function() {
this.get('/posts', function(request) {
var body, subset, perPage, startPage, offset;

if (request.queryParams.category) {
subset = posts.filter(function(post) {
subset = posts.filter(post => {
return post.category === request.queryParams.category;
});
} else {
Expand All @@ -41,16 +41,16 @@ module('Acceptance: Infinity Route', {
});
});
},
teardown: function() {
teardown() {
Ember.run(App, 'destroy');
server.shutdown();
}
});

test('it works when meta is present in payload', function(assert) {
test('it works when meta is present in payload', assert => {
visit('/');

andThen(function() {
andThen(() => {
var postsTitle = find('#posts-title');
var postList = find('ul');
var infinityLoader = find('.infinity-loader');
Expand All @@ -61,10 +61,10 @@ test('it works when meta is present in payload', function(assert) {
});
});

test('it works with parameters', function(assert) {
test('it works with parameters', assert => {
visit('/category/a?per_page=2');

andThen(function() {
andThen(() => {
var postsTitle = find('#posts-title');
var postList = find('ul');
var infinityLoader = find('.infinity-loader');
Expand Down
12 changes: 6 additions & 6 deletions tests/acceptance/infinity-route-without-meta-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ import Pretender from 'pretender';
var App, server;

module('Acceptance: Infinity Route', {
setup: function() {
setup() {
App = startApp();
server = new Pretender(function() {
this.get('/posts', function(request) {
this.get('/posts', request => {
var posts = [
{ id: 1, name: "Squarepusher" },
{ id: 2, name: "Aphex Twin" }
];
return [200, {"Content-Type": "application/json"}, JSON.stringify({posts: posts})];
return [200, {"Content-Type": "application/json"}, JSON.stringify({posts})];
});
});
},
teardown: function() {
teardown() {
Ember.run(App, 'destroy');
server.shutdown();
}
});

test('it works when meta is not present in payload', function(assert) {
test('it works when meta is not present in payload', assert => {
visit('/');

andThen(function() {
andThen(() => {
var postsTitle = find('#posts-title');
var postList = find('ul');
var infinityLoader = find('.infinity-loader');
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/app/routes/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Ember from 'ember';
import InfinityRoute from 'ember-infinity/mixins/route';

export default Ember.Route.extend(InfinityRoute, {
model: function(params) {
model(params) {
return this.infinityModel('post', { category: params.category,
perPage: 2 });
}
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/start-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function startApp(attrs) {
var attributes = Ember.merge({}, config.APP);
attributes = Ember.merge(attributes, attrs); // use defaults, but you can override;

Ember.run(function() {
Ember.run(() => {
application = Application.create(attributes);
application.setupForTesting();
application.injectTestHelpers();
Expand Down
Loading

0 comments on commit 18f2145

Please sign in to comment.