首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSONStore事件未触发

JSONStore事件未触发
EN

Stack Overflow用户
提问于 2013-04-17 02:56:41
回答 1查看 575关注 0票数 0

在过去的一周里,我已经尝试了这么多方法,但是我不能让JSONStore回调一致地触发。我在一台运行Worklight 5.0.6的Mac上工作。我将提供我认为是最简单的示例:使用硬编码值从Worklight JSONStore生成器创建的代码。

初始化集合并调用findAll()之类的函数后,既不会触发成功回调,也不会触发失败回调。我已经让回调起作用了,但不是始终如一的或可靠的。我无法解释为什么它大多不起作用。

我已经尝试过使用jQuery promise框架和过时的回调方法。在这两种情况下,都不会调用回调。我还尝试在JSONStore上调用destroy()来强制重新创建。

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2013-04-17 04:15:54

您的JavaScript中有许多错误,您调用的是customers.load而不是contacts.load,并且您拥有的承诺嵌套可能不是您想要的。我没有你的适配器,所以我不能百分之百确保这段代码可以工作,但它会非常接近你想要的。

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16044908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档