user2217751
user2217751

Reputation: 485

JSONStore events not firing

I have tried this many, many ways over the last week, but I cannot get the JSONStore callbacks to fire consistently. I'm working on a Mac running Worklight 5.0.6. I'll provide what I think is the most simple example: code created from the Worklight JSONStore generator with hard-coded values.

After initializing the collections and calling a function like findAll() neither the success nor the failure callbacks are triggered. I have had the callbacks work, but not consistently or reliably. I cannot explain why it mostly doesn't work.

I have tried this both using the jQuery promise framework and the deprecated callback methods. In both cases the callbacks aren't getting called. I have also tried calling destroy() on the JSONStore to force fresh creation.

function initContactsCollection() {

if ((WL.Client.getEnvironment() === 'iphone' ||
WL.Client.getEnvironment() === 'ipad' ||
WL.Client.getEnvironment() === 'android') &&
typeof cordova !== 'undefined' &&
typeof WL.JSONStore !== 'undefined' &&
typeof jQuery !== 'undefined' &&
typeof jQuery.event.trigger === 'function') {

// var pwd = prompt('Enter your password');

var contacts = WL.JSONStore.initCollection("contacts",
        {"agentId":"string","notes.isSuccessful":"boolean","firstName":"string","workPhone":"string","email1":"string","email2":"string"},
    {
        //password: pwd,

        adapter : {
            name: 'ams',
            replace: 'updateContact',
            remove: 'deleteContactNote',
            add: 'addNewContact',
            load: {
                procedure: 'getContacts',
                params: ["AA12345678910X-DB"],
                key: 'contacts'
            },
            accept: function (data) {
                return (data.status === 200);
            }
        }
    });

contacts.promise

.done(function () {
    WL.Logger.debug('[JSONStore] contacts is ready to be used.');

    contacts.count().done(function(res){
        if(res < 1){
            customers.load();
        }
    });
})

.fail(function (errObj) {
    WL.Logger.debug('[JSONStore]' + errObj.toString());
});

} else {
    WL.Logger.debug('[JSONStore] Check your dependencies.');
}


} // end function

Cordova console

Upvotes: 0

Views: 583

Answers (1)

rooftop
rooftop

Reputation: 3027

There are a number of errors in your JavaScript, you're calling customers.load instead of contacts.load, and the nesting of promises that you have, probably isn't what you want. I don't have your adapter so I can't make 100% sure this code works, but it'll be much close to what you want.

function initContactsCollection() {

    if ((WL.Client.getEnvironment() === 'iphone' ||
        WL.Client.getEnvironment() === 'ipad' ||
        WL.Client.getEnvironment() === 'android') &&
        typeof cordova !== 'undefined' &&
        typeof WL.JSONStore !== 'undefined' &&
        typeof jQuery !== 'undefined' &&
        typeof jQuery.event.trigger === 'function') {

// var pwd = prompt('Enter your password');

    var contacts = WL.JSONStore.initCollection("contacts",
        {"agentId":"string","notes.isSuccessful":"boolean","firstName":"string","workPhone":"string","email1":"string","email2":"string"},
        {
            //password: pwd,

            adapter : {
                name: 'ams',
                replace: 'updateContact',
                remove: 'deleteContactNote',
                add: 'addNewContact',
                load: {
                    procedure: 'getContacts',
                    params: ['AA12345678910X-DB'],
                    key: 'contacts'
                },
                accept: function (data) {
                    return (data.status === 200);
                }
            }
        });

        contacts.promise

        .then(function () {
            WL.Logger.debug('[JSONStore] contacts is ready to be used.');

            return contacts.count();
        })
        .then(function(res){
            var newDeferred;
            if(res < 1){
                WL.Logger.debug('DB Empty, calling load');
                return contacts.load();
            }else{
                newDeferred = $.Deferred();
                setTimeout(function(){
                    newDeferred.resolve(0);
                });
                return newDeferred;
            }
        })
        .then(function(){
            WL.Logger.debug('done with setup stuff');
        })

        .fail(function (errObj) {
            WL.Logger.debug('[JSONStore]' + errObj.toString());
        });

    } else {
        WL.Logger.debug('[JSONStore] Check your dependencies.');
    }


} // end function

Upvotes: 1

Related Questions