I've got a Backbone model that has some of it's dependencies loaded via require JS. I'm handling it this way to get around the circular dependency issue with Require.js. (We have multiple files requiring models, collections, and views, and some of them are circular.)
The problem is that attributes set on the model (this
), are coming out as undefined
outside the require statement. Code is here:
define(["jquery", "backbone"],
function($, Backbone) {
var Model = Backbone.Model.extend({
initialize: function(options) {
var that = this;
require(["collections/agenciesCollection", "collections/usersCollection", "models/userModel"], function(Agencies, Users, User) {
that.agencies = (!options || !options.agencies) ? new Agencies() : new Agencies(options.agencies);
that.users = (!options || options.users) ? new Users() : new Users(options.users);
if(!options || !options.contact) that.set("contact", new User()); else that.set("contact", new User(options.contact));
if(!options || !options.admin) that.set("admin", new User()); else that.set("admin", new User(options.contact));
console.log(that.agencies); // This is set to a collection
});
console.log(this.agencies); // This is 'undefined'
console.log(this); // This has an attribute "agencies" listed in Chrome inspector
}
return Model;
});