Last active
August 29, 2015 14:13
-
-
Save huafu/f9f15f182efad7b51e47 to your computer and use it in GitHub Desktop.
Basic session handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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');//... | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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!