Sencha Touch 2: belongsTo association callback usage?

百般思念 提交于 2019-12-13 02:11:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!