Skip to content

Commit

Permalink
Support when fastboot does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbieTheWagner committed Aug 19, 2016
1 parent 3f1f472 commit 86af512
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
9 changes: 6 additions & 3 deletions addon/mixins/reset-scroll.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import Ember from 'ember';
const { $, inject: { service }, Mixin } = Ember;
import getOwner from 'ember-getowner-polyfill';
const { $, computed, Mixin } = Ember;

export default Mixin.create({
fastboot: service(),
fastboot: computed(function() {
return getOwner(this).lookup('service:fastboot');
}),
/**
* Scroll to top when route is entered.
*/
activate(...args) {
this._super(...args);
if (!this.get('fastboot.isFastBoot')) {
if(!this.get('fastboot') || !this.get('fastboot.isFastBoot')) {
$(window).scrollTop(0);
}
}
Expand Down
59 changes: 30 additions & 29 deletions addon/mixins/scroll-operator.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import Ember from 'ember';
const { $, inject: { service }, Mixin, run } = Ember;
import getOwner from 'ember-getowner-polyfill';
const { $, computed, Mixin, run } = Ember;

export default Mixin.create({
_scrollingTimeout: 100,

fastboot: service(),
fastboot: computed(function() {
return getOwner(this).lookup('service:fastboot');
}),

/**
* Attach on-scroll handler to window/document. Handler will call _scrollTop
* on scroll.
*/
activate(...args) {
this._super(...args);
if (!this.get('fastboot.isFastBoot')) {
this._attachEvents();
}
this._attachEvents();
},

/**
* Detach on-scroll handlers on route exit.
*/
deactivate(...args) {
this._super(...args);
if (!this.get('fastboot.isFastBoot')) {
this._detachEvents();
}
this._detachEvents();
},

/**
Expand All @@ -37,13 +36,11 @@ export default Mixin.create({

this._super(...args);

if (!this.get('fastboot.isFastBoot')) {
if (!this._didTransitionViaBackOrForward(transition) && this.controller) {
this.controller.set('currentPosition', 0);
}

this._detachEvents();
if (!this._didTransitionViaBackOrForward(transition) && this.controller) {
this.controller.set('currentPosition', 0);
}

this._detachEvents();
},

/**
Expand All @@ -56,22 +53,22 @@ export default Mixin.create({

this._super(...args);

if (!this.get('fastboot.isFastBoot')) {
if (controller) {
run.schedule('afterRender', null, () => {
$(window).scrollTop(controller.getWithDefault('currentPosition', 0));
this._attachEvents();
});
}
if (controller && (!this.get('fastboot') || !this.get('fastboot.isFastBoot'))) {
run.schedule('afterRender', null, () => {
$(window).scrollTop(controller.getWithDefault('currentPosition', 0));
this._attachEvents();
});
}
},

_attachEvents() {
const onScroll = () => {
run.debounce(this, this._setScrollTop, this._scrollingTimeout);
};
$(document).on('touchmove.scrollable', onScroll);
$(window).on('scroll.scrollable', onScroll);
if(!this.get('fastboot') || !this.get('fastboot.isFastBoot')) {
const onScroll = () => {
run.debounce(this, this._setScrollTop, this._scrollingTimeout);
};
$(document).on('touchmove.scrollable', onScroll);
$(window).on('scroll.scrollable', onScroll);
}
},

/**
Expand All @@ -83,14 +80,18 @@ export default Mixin.create({
},

_detachEvents() {
$(document).off('.scrollable');
$(window).off('.scrollable');
if(!this.get('fastboot') || !this.get('fastboot.isFastBoot')) {
$(document).off('.scrollable');
$(window).off('.scrollable');
}
},

/**
* Set currentPosition to $(window).scrollTop value.
*/
_setScrollTop() {
this.set('controller.currentPosition', $(window).scrollTop());
if(!this.get('fastboot') || !this.get('fastboot.isFastBoot')) {
this.set('controller.currentPosition', $(window).scrollTop());
}
}
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"ember-data": "^2.7.0",
"ember-disable-prototype-extensions": "^1.1.0",
"ember-export-application-global": "^1.0.5",
"ember-getowner-polyfill": "1.0.1",
"ember-load-initializers": "^0.5.1",
"ember-resolver": "^2.0.3",
"loader.js": "^4.0.1"
Expand Down

0 comments on commit 86af512

Please sign in to comment.