I'm trying to run a bit of code when my loginManager is logged in. It might be already, or I might be waiting:
var loginManager = chrome.extension.getBackgroundPage().LoginManager;
// If the foreground is opened before the background has had a chance to load, wait for the background.
// This is easier than having every control on the foreground guard against the background not existing.
if (loginManager.get('loggedIn')) {
// Load foreground when the background indicates it has loaded.
require(['foreground']);
} else {
loginManager.once('loggedIn', function () {
// Load foreground when the background indicates it has loaded.
require(['foreground']);
});
}
and here's how loginManager looks:
// TODO: Exposed globally for the foreground. Is there a better way?
var LoginManager = null;
define(['user'], function(User) {
'use strict';
var loginManagerModel = Backbone.Model.extend({
defaults: {
loggedIn: false,
user: null
},
login: function() {
if (!this.get('loggedIn')) {
var user = new User();
var self = this;
user.on('loaded', function() {
self.set('loggedIn', true);
self.set('user', this);
self.trigger('loggedIn');
});
}
}
});
LoginManager = new loginManagerModel();
return LoginManager;
});
I was hoping to try out jquery's .when() as it seemed like it might be applicable here, but I wasn't sure if this was the right scenario since it does not involve AJAX request explicitly --- they're deeper down and at this level I am just triggering custom events.