Note that there are some explanatory texts on larger screens.

plurals
  1. PObackbone.js Model.get() returns undefined, scope using coffeescript + coffee toaster?
    primarykey
    data
    text
    <p>I'm writing an app using coffeescript with coffee toaster (an awesome NPM module for stitching) that builds my app.js file. </p> <p>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). </p> <p>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:</p> <pre><code>@user = new models.User @user.fetch() console.log(@user) console.log(@user.get('email')) </code></pre> <p>The first logging statement outputs the correct Backbone.Model attributes object in the console just as it should: </p> <pre><code>User _changing: false _escapedAttributes: Object _pending: Object _previousAttributes: Object _silent: Object attributes: Object account: Object created_on: "1983-12-13 00:00:00" email: "ben@accomplicecreative.com" 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 </code></pre> <p>However, the second returns <em>undefined</em> despite the model attributes clearly being there in the console when logged. </p> <p>And just to make things even more interesting, typing "window.app.user.get('email')" into the console manually returns the expected value of "ben@accomplicecreative.com"... ?</p> <p>Just for reference, here's how the initialize method compiles into my app.js file:</p> <pre><code>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; }; </code></pre> <p>I initialize the Application controller in my static HTML like so: </p> <pre><code>jQuery(document).ready(function(){ window.app = new controllers.Application(); }); </code></pre> <p>Suggestions please and thank you!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload