extjs 商店中的成功和失败函数- Ext JS

tnkciper  于 2023-11-18  发布在  其他
关注(0)|答案(1)|浏览(157)

我有一个请求,在成功时,循环遍历JSON响应的每个属性,并将其添加到我的store

  1. var request = Ext.Ajax.request({
  2. url: 'MCApp',
  3. jsonData: searchquery,
  4. params: {
  5. start: 0,
  6. limit: itemsPerPage
  7. },
  8. success: function(response) {
  9. mainresponse = response.responseText;
  10. if (mainresponse.length == 0) {
  11. alert('No results returned');
  12. return;
  13. }
  14. var decoded = Ext.decode(mainresponse);
  15. for (var i = 0; i < decoded.elements.length; i++) { // loop over decoded data
  16. var element = decoded.elements[i].element;
  17. var model = {};
  18. for (var x = 0; x < element.attributes.length; x++) { // loop over attributes
  19. var attribute = element.attributes[x];
  20. model[attribute.attrname] = attribute.attrvalue; // mapping element names & attributes
  21. }
  22. newstore.add(model); // implicitly cast data as Model
  23. models[i] = model;
  24. }
  25. newstore.loadRawData(models);
  26. },
  27. failure: function() {
  28. alert('Search Failed: Could not reach the server')
  29. }
  30. });

字符串
我现在已经在我的store中重新创建了上面的request。我需要做的是添加这些相同的成功和失败函数。

  1. var store = Ext.create('Ext.data.Store', {
  2. storeId: 'resultsetstore',
  3. autoLoad: false,
  4. pageSize: itemsPerPage,
  5. fields: [
  6. { name: 'id', type: 'auto' },
  7. { name: 'name', type: 'auto' },
  8. { name: 'description', type: 'auto' }
  9. ],
  10. proxy: {
  11. type: 'ajaxwithpayload', //customized proxy to read "jsonData"
  12. url: 'MCApp',
  13. jsonData: searchquery,
  14. reader: {
  15. type: 'json',
  16. root: 'elements'
  17. }
  18. success: { /* success functions */ },
  19. failure: { /* failure functions */ }
  20. }
  21. });


以下是我的回应:

  1. {
  2. "elements":[
  3. {
  4. "element":{
  5. "name":"Element Name",
  6. "id":"Element ID",
  7. "attributes":[
  8. {
  9. "attrname":"id",
  10. "attrvalue":"This is the ID"
  11. },
  12. {
  13. "attrname":"name",
  14. "attrvalue":"This is the name"
  15. },
  16. //etc.


1)有没有办法在我的商店中重新创建这些功能?
2)这样解码我的响应是将我的响应加载到存储中的最佳方法吗?

编辑

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

  1. store.load({
  2. params: { start: 0, limit: itemsPerPage },
  3. callback: function(options, success, response, records) {
  4. if (success) {
  5. alert(response.responseText);
  6. }
  7. }
  8. });


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

ax6ht2ek

ax6ht2ek1#

存储中的错误处理

您可以在代理上侦听异常事件以捕获所有存储错误,并在存储加载事件上侦听成功

  1. var store = Ext.create('Ext.data.Store', {
  2. storeId: 'resultsetstore',
  3. autoLoad: false,
  4. pageSize: itemsPerPage,
  5. fields: [
  6. { name: 'id', type: 'auto' },
  7. { name: 'name', type: 'auto' },
  8. { name: 'description', type: 'auto' }
  9. ],
  10. listeners: {
  11. load: function(store, records, successful, eOpts) {
  12. if (successfull) {
  13. alert('success');
  14. }
  15. }
  16. },
  17. proxy: {
  18. type: 'ajaxwithpayload', //customized proxy to read "jsonData"
  19. url: 'MCApp',
  20. jsonData: searchquery,
  21. reader: {
  22. type: 'json',
  23. root: 'elements'
  24. },
  25. listeners: {
  26. exception: function(proxy, response, operation, eOpts) {
  27. alert('exception');
  28. }
  29. }
  30. }
  31. });

字符串
或者在load调用本身中:

  1. store.load({
  2. callback: function(records, operation, success) {
  3. // ...
  4. }
  5. });


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

  1. store.sync({
  2. callback: function(batch, options) {
  3. // ...
  4. },
  5. success: function(batch, options) {
  6. // ...
  7. },
  8. failure: function(batch, options) {
  9. // ...
  10. }
  11. });

展开查看全部

相关问题