XmlSimlet.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * This class simulates XML-based requests.
  3. */
  4. Ext.define('Ext.ux.ajax.XmlSimlet', {
  5. extend: 'Ext.ux.ajax.DataSimlet',
  6. alias: 'simlet.xml',
  7. /**
  8. * This template is used to populate the XML response. The configuration of the Reader
  9. * is available so that its `root` and `record` properties can be used as well as the
  10. * `fields` of the associated `model`. But beyond that, the way these pieces are put
  11. * together in the document requires the flexibility of a template.
  12. */
  13. xmlTpl: [
  14. '<{root}>\n',
  15. '<tpl for="data">',
  16. ' <{parent.record}>\n',
  17. '<tpl for="parent.fields">',
  18. ' <{name}>{[parent[values.name]]}</{name}>\n',
  19. '</tpl>',
  20. ' </{parent.record}>\n',
  21. '</tpl>',
  22. '</{root}>'
  23. ],
  24. doGet: function (ctx) {
  25. var me = this,
  26. data = me.getData(ctx),
  27. page = me.getPage(ctx, data),
  28. reader = ctx.xhr.options.proxy.reader,
  29. ret = me.callParent(arguments), // pick up status/statusText
  30. response = {
  31. data: page,
  32. reader: reader,
  33. fields: reader.model.getFields(),
  34. root: reader.root,
  35. record: reader.record
  36. };
  37. if (ctx.groupSpec) {
  38. response.summaryData = me.getSummary(ctx, data, page);
  39. }
  40. var tpl = Ext.XTemplate.getTpl(me, 'xmlTpl'),
  41. xml = tpl.apply(response),
  42. doc;
  43. if (typeof DOMParser != 'undefined') {
  44. doc = (new DOMParser()).parseFromString(xml, "text/xml");
  45. } else {
  46. // IE doesn't have DOMParser, but fortunately, there is an ActiveX for XML
  47. doc = new ActiveXObject("Microsoft.XMLDOM");
  48. doc.async = false;
  49. doc.loadXML(xml);
  50. }
  51. ret.responseText = xml;
  52. ret.responseXML = doc;
  53. return ret;
  54. }
  55. });