Skip to content

Instantly share code, notes, and snippets.

@huafu
Last active August 29, 2015 14:13
Show Gist options
  • Save huafu/f9f15f182efad7b51e47 to your computer and use it in GitHub Desktop.
Save huafu/f9f15f182efad7b51e47 to your computer and use it in GitHub Desktop.
Basic session handling
export function initialize(container, application) {
application.inject('service:session', 'store', 'store:main');
application.inject('route', 'session', 'service:session');
application.inject('controller', 'session', 'service:session');
application.inject('model', 'session', 'service:session');
application.inject('component', 'session', 'service:session');
application.deferReadiness();
// load the current session
container.lookup('service:session').load().then(function () {
application.advanceReadiness();
});
}
/**
* @class SessionServiceInitializer
*/
export default {
name: 'session-service',
after: 'store',
initialize: initialize
};
import DS from 'ember-data';
/**
* @class Session
* @extends DS.Model
*/
export default DS.Model.extend({
/**
* The user owning the session
* @property user
* @type {User}
*/
user: DS.belongsTo('user')
});
// that can be any controller, route, model or component since we injected in all of them
import Ember from 'ember';
export default Ember.Route.extend({
// any method where you need to access the session service:
someMethod: function () {
// this.session is the service object, and since we extended it from ObjectController, it acts as a proxy for the session record:
this.session.get('user.name');//...
}
});
import Ember from 'ember';
export default Ember.ObjectController.extend({
isAuthenticated: Ember.computed.bool('model.user.isClaimed'),
load: function () {
var _this = this;
return this.store.find('session', yourSessionCookieOrWhatever).then(function (session) {
_this.set('model', session);
return _this;
}).catch(function () {
// create a session or handle the way you handle no session with your app
return _this;
});
}
});
{{! since we injected our service on the controllers, we have direct access to it with `session`}}
{{#if session.isAuthenticated}}
Logged in as {{session.user.name}}
{{/if}}
@hoIIer
Copy link

hoIIer commented Jan 21, 2015

@huafu, thanks for writing this, spent many hours trying to get a solution working and finally got one working... https://github.com/erichonkanen/cl-frontend/blob/master/app/initializers/session.coffee

However the magic was that I discovered you can use "selectionBinding" instead of "value=" in the select view.. that fixed it and made it work for me... I have no idea why that's not in the ember docs!!! Not even sure whats different

https://github.com/erichonkanen/cl-frontend/blob/master/app/templates/application.hbs#L23

Thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment