问题
I call a belongsTo assocation generated getter function:
token.getUser({
callback: function(user, operation) {
console.info('getUser callback');
Ext.Viewport.unmask();
console.dir(operation);
},
failure: function(user, operation) {
console.info('getUser failure');
},
success: function(user) {
console.info('getUser success');
}
});
My console shows the following:
getUser success Auth.js:74
getUser failure Auth.js:69
getUser callback Auth.js:64
undefined
Can someone please enlighten me how this works? The docs doesn't help much.
回答1:
Most certainly a bug. Look at the code of the createGetter method:
if (options.reload === true || instance === undefined) {
...
} else {
args = [instance];
scope = scope || model;
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;
}
If the associated model has already been loaded, all callbacks are called indiscriminately.
You can use the following workaround, as we see that the second argument is not called for cached instances:
token.getUser({
callback: function(user, operation) {
// independent of success
if (operation) {
if (operation.wasSuccessful()) {
// successful load
} else {
// failed load
}
} else {
// retrieved from the cache (indicates previous success)
}
}
});
来源:https://stackoverflow.com/questions/17063029/sencha-touch-2-belongsto-association-callback-usage