return function(options, scope) {
options = options || {};
- var foreignKeyId = this.get(foreignKey),
- instance, callbackFn;
+ var model = this,
+ foreignKeyId = model.get(foreignKey),
+ instance,
+ args;
- if (this[instanceName] === undefined) {
+ if (model[instanceName] === undefined) {
instance = Ext.ModelManager.create({}, associatedName);
instance.set(primaryKey, foreignKeyId);
if (typeof options == 'function') {
options = {
callback: options,
- scope: scope || this
+ scope: scope || model
};
}
associatedModel.load(foreignKeyId, options);
+ model[instanceName] = associatedModel;
+ return associatedModel;
} else {
- instance = this[instanceName];
-
+ instance = model[instanceName];
+ args = [instance];
+ scope = scope || model;
+
//TODO: We're duplicating the callback invokation code that the instance.load() call above
//makes here - ought to be able to normalize this - perhaps by caching at the Model.load layer
//instead of the association layer.
- if (typeof options == 'function') {
- options.call(scope || this, instance);
- }
-
- if (options.success) {
- options.success.call(scope || this, instance);
- }
-
- if (options.callback) {
- options.callback.call(scope || this, instance);
- }
+ Ext.callback(options, scope, args);
+ Ext.callback(options.success, scope, args);
+ Ext.callback(options.failure, scope, args);
+ Ext.callback(options.callback, scope, args);
return instance;
}