Start a new topic

Using Backbone with the activeUser


I am trying to do the following:

Listen for the "change" event on the Backbone.getActiveUser() - but the event is never fired. What is the mistake in this case?

Listening like: Kinvey.Backbone.getActiveUser().on('change')




getActiveUser() returns a promise, so you could just wait for the promise to resolve before taking action, since promises are asynchronous in nature.

You can see some sample code over here:

Does this make sense to you?  


Hey Damien, thanks for you answer - no not really make sense (to me ;) )

I just want to listen to events on this user like this:


the same way i can listen to it, if it were a new model or collection.

If it's returning a promise, - how can i access the raw user?

Best, Nico


Have you ever worked with promises?   The general concept is something like this.

You take an item which is going to require an asynchronous operation to complete (network request, db call, etc).   This gets called in a function.  For example in the javascript (html5) library, You might call Kinvey.Datastore.Save();

You could make that look something like this... 

var promise ='firstNames', {
'firstName': firstName
promise.then(function (success) {
alert("Success: " + success.firstName + " added to database");
}, function (error) {
alert("Error saving to firstNames: " + error.description);

The .then() means to call the function and so long as you are in the success / failure parts of the promise, you will wait for the call to complete and return the object (success / error in this case would be the json response or the json error response from the server).

You can't await a change on a promise because a promise is not part of the DOM.  Furthermore, there shouldn't be too many reasons why an activeUser changes (only on login / logout), which can be handled in the callbacks of those functions far more efficiently than adding an eventlistener to the activeUser object.

Does this make more sense?



thanks for me detailed Answer - your last Point, I'm not sure about this.

In my application i have a typically "slidemenu" with basic profil Informations of the current User. Let's assume he/she is in the profileWindow and edit the fullname. With changelisteners on the User Model i don't have to do any more, but without i have to send "custom" Events through my application to "notify" my left Slide Window to update the display.

In my understanding this isn'the best solution, or? And Backbone does provide those Events on the models/collection, so I think we should use them.

What do you think?


Besides: Maybe for me isn't clear, whats the activeUser() function is build for. I thought it's just a reference to the currentLogged in User to acces data. I mean i could store it inside an alloy variable or so and acces the events with ease.

Answer would be great.

Best, Nico

Login or Signup to post a comment