Skip to content

Commit

Permalink
Merge pull request #917 from simplabs/cleanup-init-methods
Browse files Browse the repository at this point in the history
Prefer overriding init over on('init', …
  • Loading branch information
marcoow committed Feb 29, 2016
2 parents 823aa9d + 30a544e commit 4259dc6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
7 changes: 4 additions & 3 deletions addon/internal-session.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ember from 'ember';
import getOwner from 'ember-getowner-polyfill';

const { RSVP, on, isNone, isEmpty } = Ember;
const { RSVP, isNone, isEmpty } = Ember;

export default Ember.ObjectProxy.extend(Ember.Evented, {
authenticator: null,
Expand All @@ -12,6 +12,7 @@ export default Ember.ObjectProxy.extend(Ember.Evented, {
init() {
this._super(...arguments);
this.set('content', { authenticated: {} });
this._bindToStoreEvents();
},

authenticate(authenticatorFactory, ...args) {
Expand Down Expand Up @@ -156,7 +157,7 @@ export default Ember.ObjectProxy.extend(Ember.Evented, {
});
},

_bindToStoreEvents: on('init', function() {
_bindToStoreEvents() {
this.store.on('sessionDataUpdated', (content) => {
let { authenticator: authenticatorFactory } = (content.authenticated || {});
if (!!authenticatorFactory) {
Expand All @@ -176,7 +177,7 @@ export default Ember.ObjectProxy.extend(Ember.Evented, {
this._clearWithContent(content, true);
}
});
}),
},

_lookupAuthenticator(authenticator) {
return getOwner(this).lookup(authenticator);
Expand Down
11 changes: 8 additions & 3 deletions addon/mixins/application-route-mixin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ember from 'ember';
import Configuration from './../configuration';

const { inject, on } = Ember;
const { inject } = Ember;

/**
The mixin for the application route; __defines methods that are called when
Expand Down Expand Up @@ -52,7 +52,12 @@ export default Ember.Mixin.create({
*/
session: inject.service('session'),

_subscribeToSessionEvents: on('init', function() {
init() {
this._super(...arguments);
this._subscribeToSessionEvents();
},

_subscribeToSessionEvents() {
Ember.A([
['authenticationSucceeded', 'sessionAuthenticated'],
['invalidationSucceeded', 'sessionInvalidated']
Expand All @@ -61,7 +66,7 @@ export default Ember.Mixin.create({
this[method](...arguments);
}));
});
}),
},

/**
This method handles the session's
Expand Down
11 changes: 8 additions & 3 deletions addon/services/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import getOwner from 'ember-getowner-polyfill';

const SESSION_DATA_KEY_PREFIX = /^data\./;

const { computed, on } = Ember;
const { computed } = Ember;

/**
__The session service provides access to the current session as well as
Expand Down Expand Up @@ -114,6 +114,11 @@ export default Ember.Service.extend(Ember.Evented, {
*/
attemptedTransition: computed.alias('session.attemptedTransition'),

init() {
this._super(...arguments);
this._forwardSessionEvents();
},

set(key, value) {
const setsSessionData = SESSION_DATA_KEY_PREFIX.test(key);
if (setsSessionData) {
Expand All @@ -124,7 +129,7 @@ export default Ember.Service.extend(Ember.Evented, {
}
},

_forwardSessionEvents: on('init', function() {
_forwardSessionEvents() {
Ember.A([
'authenticationSucceeded',
'invalidationSucceeded'
Expand All @@ -137,7 +142,7 @@ export default Ember.Service.extend(Ember.Evented, {
});
}
});
}),
},

/**
__Authenticates the session with an `authenticator`__ and appropriate
Expand Down
8 changes: 5 additions & 3 deletions addon/session-stores/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Ember from 'ember';
import BaseStore from './base';
import objectsAreEqual from '../utils/objects-are-equal';

const { RSVP, computed, on, run: { next } } = Ember;
const { RSVP, computed, run: { next } } = Ember;

/**
Session store that persists data in a cookie.
Expand Down Expand Up @@ -87,13 +87,15 @@ export default BaseStore.extend({
return visibilityState === 'visible';
}).volatile(),

_setup: on('init', function() {
init() {
this._super(...arguments);

next(() => {
this._syncData().then(() => {
this._renewExpiration();
});
});
}),
},

/**
Persists the `data` in the cookie.
Expand Down
7 changes: 4 additions & 3 deletions addon/session-stores/ephemeral.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ember from 'ember';
import BaseStore from './base';

const { RSVP, on } = Ember;
const { RSVP } = Ember;

/**
Session store that __persists data in memory and thus is not actually
Expand All @@ -15,9 +15,10 @@ const { RSVP, on } = Ember;
@public
*/
export default BaseStore.extend({
_setup: on('init', function() {
init() {
this._super(...arguments);
this.clear();
}),
},

/**
Persists the `data`. This replaces all currently stored data.
Expand Down

0 comments on commit 4259dc6

Please sign in to comment.