_DataListMixin.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. define("dojox/mobile/_DataListMixin", [
  2. "dojo/_base/array",
  3. "dojo/_base/connect",
  4. "dojo/_base/declare",
  5. "dojo/_base/lang",
  6. "dijit/registry", // registry.byId
  7. "./ListItem"
  8. ], function(array, connect, declare, lang, registry, ListItem){
  9. // module:
  10. // dojox/mobile/_DataListMixin
  11. // summary:
  12. // Mixin for widgets to generate the list items corresponding to the
  13. // data provider object.
  14. return declare("dojox.mobile._DataListMixin", null,{
  15. // summary:
  16. // Mixin for widgets to generate the list items corresponding to
  17. // the data provider object.
  18. // description:
  19. // By mixing this class into the widgets, the list item nodes are
  20. // generated as the child nodes of the widget and automatically
  21. // re-generated whenever the corresponding data items are modified.
  22. // store: Object
  23. // Reference to data provider object
  24. store: null,
  25. // query: Object
  26. // A query that can be passed to 'store' to initially filter the
  27. // items.
  28. query: null,
  29. // queryOptions: Object
  30. // An optional parameter for the query.
  31. queryOptions: null,
  32. buildRendering: function(){
  33. this.inherited(arguments);
  34. if(!this.store){ return; }
  35. var store = this.store;
  36. this.store = null;
  37. this.setStore(store, this.query, this.queryOptions);
  38. },
  39. setStore: function(store, query, queryOptions){
  40. // summary:
  41. // Sets the store to use with this widget.
  42. if(store === this.store){ return; }
  43. this.store = store;
  44. this.query = query;
  45. this.queryOptions = queryOptions;
  46. if(store && store.getFeatures()["dojo.data.api.Notification"]){
  47. array.forEach(this._conn || [], connect.disconnect);
  48. this._conn = [
  49. connect.connect(store, "onSet", this, "onSet"),
  50. connect.connect(store, "onNew", this, "onNew"),
  51. connect.connect(store, "onDelete", this, "onDelete")
  52. ];
  53. }
  54. this.refresh();
  55. },
  56. refresh: function(){
  57. // summary:
  58. // Fetches the data and generates the list items.
  59. if(!this.store){ return; }
  60. this.store.fetch({
  61. query: this.query,
  62. queryOptions: this.queryOptions,
  63. onComplete: lang.hitch(this, "onComplete"),
  64. onError: lang.hitch(this, "onError")
  65. });
  66. },
  67. createListItem: function(/*Object*/item){
  68. // summary:
  69. // Creates a list item widget.
  70. var attr = {};
  71. var arr = this.store.getLabelAttributes(item);
  72. var labelAttr = arr ? arr[0] : null;
  73. array.forEach(this.store.getAttributes(item), function(name){
  74. if(name === labelAttr){
  75. attr["label"] = this.store.getLabel(item);
  76. }else{
  77. attr[name] = this.store.getValue(item, name);
  78. }
  79. }, this);
  80. var w = new ListItem(attr);
  81. item._widgetId = w.id;
  82. return w;
  83. },
  84. generateList: function(/*Array*/items, /*Object*/dataObject){
  85. // summary:
  86. // Given the data, generates a list of items.
  87. array.forEach(this.getChildren(), function(child){
  88. child.destroyRecursive();
  89. });
  90. array.forEach(items, function(item, index){
  91. this.addChild(this.createListItem(item));
  92. }, this);
  93. },
  94. onComplete: function(/*Array*/items, /*Object*/request){
  95. // summary:
  96. // An handler that is called after the fetch completes.
  97. this.generateList(items, request);
  98. },
  99. onError: function(/*Object*/errorData, /*Object*/request){
  100. // summary:
  101. // An error handler.
  102. },
  103. onSet: function(/*Object*/item, /*String*/attribute, /*Object|Array*/oldValue, /*Object|Array*/newValue){
  104. // summary:
  105. // See dojo.data.api.Notification.onSet()
  106. },
  107. onNew: function(/*Object*/newItem, /*Object?*/parentInfo){
  108. // summary:
  109. // See dojo.data.api.Notification.onNew()
  110. this.addChild(this.createListItem(newItem));
  111. },
  112. onDelete: function(/*Object*/deletedItem){
  113. // summary:
  114. // See dojo.data.api.Notification.onDelete()
  115. registry.byId(deletedItem._widgetId).destroyRecursive();
  116. }
  117. });
  118. });