Tjorriemorrie
Tjorriemorrie

Reputation: 17282

Sencha Touch 2: belongsTo association callback usage?

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.

Upvotes: 1

Views: 246

Answers (1)

rixo
rixo

Reputation: 25011

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)
        }
    }
});

Upvotes: 2

Related Questions