Last active
December 19, 2015 14:29
-
-
Save kelso/5969817 to your computer and use it in GitHub Desktop.
This file contains 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
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') |
This file contains 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
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