lundi 16 février 2015

Ember 'currentUser' Model/Controller Setup


Ok, so here's what I'm trying to accomplish (in ember.js):



  • New model/controller to manage the current user and session information

  • The model needs to be available everywhere so I can just do something like currentUser.firstname (for instance, in the nav)


After lots and lots of research, it seems that setting up a separate controller/model is the best way to go. I tried doing everything in the application controller, but then I need to implicitly set the user model (somehow?) on that controller, which doesn't seem like a good idea (what if I need to do other things in the application controller?).


So here's what I've tried:


controllers/session.js



init: function() {
// this never gets called unless I call the setCurrentUser
// function from another controller using 'needs' or something
},

setCurrentUser: function() {
// you can ignore the authData.uid variable - it's something used
// by firebase, but not important for this example. Just assume I'm
// requesting and getting a user back.
this.store.find('user').then(function(users) {
this.set('currentUser', users.filterBy('uid', authData.uid)[0])
}
}


models/session.js



DS.Model.extend({
currentUser: DS.belongsTo('user')
});


I don't have a route or view/template associated with this model/controller because it really wouldn't make sense (the user doesn't need to see a page all about him/herself).


So I tried calling setCurrentUser from the application controller by doing something like this.get('setCurrentUser')() (which looks very weird - there's got to be a better way, but I think the answer may be not calling this from the application controller and initializing the controller in a different way?).


I would love some advice on how I can get this working. Sorry that I'm trying to develop and explain the architecture of the app all at the same time, so it is a bit messy - please let me know if anything is unclear.


Thanks for the help!





Aucun commentaire:

Enregistrer un commentaire