ajax Extjs 存储加载成功处理程序没有被触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13353236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:52:03  来源:igfitidea点击:

Extjs store load success handler not getting fired

ajaxextjsloadextjs4store

提问by BrynJ

I have a store load method which returns data via an ajax request. I can see that the data is being returned using Firebug, but my success handler is not getting called:

我有一个存储加载方法,它通过 ajax 请求返回数据。我可以看到正在使用 Firebug 返回数据,但没有调用我的成功处理程序:

    this.getCategoriesStore().load({params:{'id':d.data.category_id}}, {
        success: function(category) {
            console.log("Category: " + category.get('name'));
        },
        error: function(e) {
            console.log(e);
        }
    });

I am returning a success parameter, along with the data:

我正在返回一个成功参数以及数据:

{"success":true,"categories":{"id":5,"name":"Frying","section_id":2}}

Is there something missing or am I doing anything wrong?

有什么遗漏还是我做错了什么?

回答by sra

Well I suppose you are looking for this:

好吧,我想你正在寻找这个:

store.load({
    params:{'id':d.data.category_id},
    scope: this,
    callback: function(records, operation, success) {
        if (success) {
            console.log("Category: " + category.get('name'));
        } else {
            console.log('error');
        }
    }
});

It is not that obvious in the APIthat your additional params can be placed there too. But ExtJS often uses the config objects to wrap things up.

API 中,您的附加参数也可以放在那里并不那么明显。但是 ExtJS 经常使用配置对象来包装东西。

Edit to answer comment:

编辑以回答评论:

The short answer is: Yes

简短的回答是:是的

Now the longer version: In case of the store it is up to you to directly provide anonymous (or concrete) callbacks or register events. Both will work the same in your situation here.

现在是更长的版本:在 store 的情况下,您可以直接提供匿名(或具体)回调或注册事件。在您的情况下,两者都将起作用。

But you can only have one callback while you can have many events. In further scenarios you will find situations where events fits much better or where events are the only way at all. That will always be the case when you are listening. Here are some notes on that:

但是你只能有一个回调,而你可以有很多事件。在进一步的场景中,您会发现事件更适合的情况或事件是唯一的方法。当你在听的时候,情况总是如此。这里有一些注意事项:

  • make use of the { single: true } property when you just need a callback once. Example: store.on('load', function(s) { /* do something*/ }, scope, { single: true })The listener will be removed after it was called. This is needed cause of the use of a anonymous function, that cannot be removed.
  • make use of mon()in most cases where you bind listeners directly in class-definitions to ensure the listeners get destroyed along with the instance of the class.
  • 当您只需要回调一次时,请使用 { single: true } 属性。示例:store.on('load', function(s) { /* do something*/ }, scope, { single: true })侦听器在调用后将被删除。这是使用匿名函数的必要原因,无法删除。
  • 在大多数情况下使用mon()直接在类定义中绑定侦听器以确保侦听器与类的实例一起销毁。

Both will save you browser memory.

两者都将节省您的浏览器内存。

回答by dbrin

Try this:

尝试这个:

store.load({
    scope: this,
    callback: function(records, operation, success) {
        // the operation object
        // contains all of the details of the load operation
        console.log(records);
    }
});

http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Store-method-loadaccording to the docs there is no success and error callback.

http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Store-method-load根据文档没有成功和错误回调。

Another alternative to providing callback you can also add a "load" event listener on the store for the same effect.

提供回调的另一种选择,您还可以在商店上添加“加载”事件侦听器以获得相同的效果。