I'm writing an app using coffeescript with coffee toaster (an awesome NPM module for stitching) that builds my app.js file.
Lots of my application classes and
templates require info about the current user so I have an instance of class User (extends Backbone.Model) stored as a property of my main Application class (extends Backbone.Router).
As part of the initialization routine I grab the user from the server (which takes care of authentication, roles, account switching etc.). Here's that coffeescript:
@user = new models.User
@user.fetch()
console.log(@user)
console.log(@user.get('email'))
The first logging statement outputs the correct Backbone.Model attributes object in the console just as it should:
User
_changing: false
_escapedAttributes: Object
_pending: Object
_previousAttributes: Object
_silent: Object
attributes: Object
account: Object
created_on: "1983-12-13 00:00:00"
email: "
[email protected]"
icon: "0"
id: "1"
last_login: "2012-06-07 02:31:38"
name: "Ben Ipsen"
roles: Object
__proto__: Object
changed: Object
cid: "c0"
id: "1"
__proto__: ctor
app.js:228
However, the second returns undefined despite the model attributes clearly being there in the console when logged.
And just to make things even more interesting, typing "window.app.user.get('email')" into the console manually returns the expected value of "
[email protected]"... ?
Just for reference, here's how the initialize method compiles into my app.js file:
Application.prototype.initialize = function() {
var isMobile;
isMobile = navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/);
this.helpers = new views.DOMHelpers().initialize().setup_viewport(isMobile);
this.user = new models.User();
this.user.fetch();
console.log(this.user);
console.log(this.user.get('email'));
return this;
};
I initialize the Application controller in my static HTML like so:
jQuery(document).ready(function(){
window.app = new controllers.Application();
});
Suggestions please and thank you!