Skip to content

Instantly share code, notes, and snippets.

@kelso
Last active December 19, 2015 14:29
Show Gist options
  • Save kelso/5969817 to your computer and use it in GitHub Desktop.
Save kelso/5969817 to your computer and use it in GitHub Desktop.
<nav class="main">
{{#linkTo "index"}}Main Page{{/linkTo}}
{{#linkTo "shifts"}}My shifts{{/linkTo}}
{{#linkTo "about"}}About{{/linkTo}}
-
{{#linkTo "login"}}Login{{/linkTo}}
</nav>
{{#if currentUser.isSignedIn}}
<p>Logged user: {{currentUser.username}}</p>
{{else}}
<p>Hello anonymous</p>
{{/if}}
<hr/>
{{outlet}}
Syncin.initializer
name: 'currentUser'
initialize: (container) ->
store = container.lookup('store:main')
attributes = $('meta[name="current-user"]').attr('content')
if attributes
object = store.load(Syncin.User, JSON.parse(attributes))
user = Syncin.User.find(object.id)
controller = container.lookup('controller:currentUser').set('content', user)
container.typeInjection('controller', 'currentUser', 'controller:currentUser')
Syncin.LoginController = Ember.Controller.extend({
logout: function() {
// TODO use http DELETE
Ember.$.post('/api/v1/sessions/logout', {}).then(function(response){
alert("You've been signed out.");
});
},
login: function() {
var data = {
username: this.get('username'),
password: this.get('password')
}
var self = this;
self.set('errorMessage', null);
Ember.$.post('/api/v1/sessions', data).then(function(response){
if (response.success) {
alert("success. token=" + response.token);
// Here's the problem. It works, but variables of signed user in application template are not refreshed
user = Syncin.User.find(1); // TODO the ID you should get from response
container = Syncin.__container__; // usage of internal API, that's bad
controller = container.lookup('controller:currentUser').set('content', user);
container.typeInjection('controller', 'currentUser', 'controller:currentUser');
self.transitionToRoute("shifts");
}
else {
self.set('errorMessage', response.message)
}
})
},
reset: function() {
this.set("username", "");
this.set("password", "");
this.set("errorMessage", "");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment