【问题标题】:Success and Failure functions in a store - Ext JS商店中的成功和失败函数 - Ext JS
【发布时间】:2014-03-17 16:22:56
【问题描述】:

我有一个请求,如果成功,它将遍历 JSON 响应的每个属性并将其添加到我的store

var request = Ext.Ajax.request({
    url: 'MCApp',
    jsonData: searchquery,
    params: {
        start: 0,
        limit: itemsPerPage
    },
    success: function(response) {
        mainresponse = response.responseText;
        if (mainresponse.length == 0) {
            alert('No results returned');
            return;
        }
        var decoded = Ext.decode(mainresponse);
        for (var i = 0; i < decoded.elements.length; i++) { // loop over decoded data
            var element = decoded.elements[i].element;
            var model = {};
            for (var x = 0; x < element.attributes.length; x++) { // loop over attributes
                var attribute = element.attributes[x];
                model[attribute.attrname] = attribute.attrvalue; // mapping element names & attributes
            }
            newstore.add(model); // implicitly cast data as Model
            models[i] = model;
        }
        newstore.loadRawData(models);
    },
    failure: function() {
        alert('Search Failed: Could not reach the server')
    }
});

我现在在我的store 中重新创建了requestabove。我需要做的是添加这些相同的成功和失败函数。

var store = Ext.create('Ext.data.Store', {
    storeId: 'resultsetstore',
    autoLoad: false,
    pageSize: itemsPerPage,
    fields: [
        { name: 'id',          type: 'auto' },
        { name: 'name',        type: 'auto' },
        { name: 'description', type: 'auto' }
    ],
    proxy: {
        type: 'ajaxwithpayload', //customized proxy to read "jsonData" 
        url: 'MCApp',
        jsonData: searchquery,
        reader: {
            type: 'json',
            root: 'elements'
        }
        success: { /* success functions */ },
        failure: { /* failure functions */ }
    }

});

我的回复如下:

{
   "elements":[
      {
         "element":{
            "name":"Element Name", 
            "id":"Element ID",
            "attributes":[
               {
                  "attrname":"id",
                  "attrvalue":"This is the ID"
               },
               {
                  "attrname":"name",
                  "attrvalue":"This is the name"
               },
               //etc.

1) 有没有办法在我的商店中重新创建这些功能?

2) 以这种方式解码我的回复是否是将我的回复加载到我的商店的最佳方式?

编辑

我在加载商店时使用回调函数:

store.load({
    params: { start: 0, limit: itemsPerPage },
    callback: function(options, success, response, records) {
        if (success) {
            alert(response.responseText);
        }           
    }
});

但是,我在警报中收到 undefined,它告诉我加载了 0 条记录。但是当我在 Firebug 中查看我的响应时,我发现我的 JSON 字符串返回得很好。

【问题讨论】:

  • 为什么不看看他们网站上的例子呢?他们已经涵盖了这一点。
  • 好吧,我承认我确实有点懒惰了。这可以用callback 完成吗?
  • 如果您不确定函数返回什么,您可以执行console.log(arguments) => 它会记录所有参数的数组,参数的顺序是错误的。 :) (或者您可以查看课程文档;))
  • 嘿@VDP,再来一个! stackoverflow.com/questions/22481633/…

标签: extjs


【解决方案1】:

商店中的错误处理

您可以在代理上侦听exception-event 以捕获所有存储错误。 为了在商店里取得成功load-event

var store = Ext.create('Ext.data.Store', {
    storeId: 'resultsetstore',
    autoLoad: false,
    pageSize: itemsPerPage,
    fields: [
        { name: 'id',          type: 'auto' },
        { name: 'name',        type: 'auto' },
        { name: 'description', type: 'auto' }
    ],
    listeners: {
        load: function(store, records, successful, eOpts) {
            if (successfull) {
                alert('success');
            }
        }
    },
    proxy: {
        type: 'ajaxwithpayload', //customized proxy to read "jsonData" 
        url: 'MCApp',
        jsonData: searchquery,
        reader: {
            type: 'json',
            root: 'elements'
        },
        listeners: {
            exception: function(proxy, response, operation, eOpts) {
                alert('exception');
            }
        }
    }
});

或在加载调用本身中:

store.load({
    callback: function(records, operation, success) {
        // ...
    }
});

或者如果您使用同步(用于保存已删除、已修改...)

store.sync({
    callback: function(batch, options) {
        // ...
    },
    success: function(batch, options) {
        // ...
    },
    failure: function(batch, options) {
        // ...
    }
});

【讨论】:

  • 起初我的store.load({}) 有一些语法错误,我认为这是它的参数顺序,但现在它可以创造奇迹。再次感谢@VDP!
猜你喜欢
  • 2014-02-03
  • 2013-12-22
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
  • 2015-10-13
  • 2012-12-02
  • 1970-01-01
  • 2013-08-16
相关资源
最近更新 更多