LazyTreeGridStoreModel.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. define("dojox/grid/LazyTreeGridStoreModel", [
  2. "dojo/_base/declare",
  3. "dojo/_base/array",
  4. "dojo/_base/lang",
  5. "dijit/tree/ForestStoreModel"], function(declare, array, lang, ForestStoreModel){
  6. return declare("dojox.grid.LazyTreeGridStoreModel", ForestStoreModel, {
  7. // There are different approaches to get children for client-side
  8. // DataStore (e.g. dojo.data.ItemFileReadStore) or server-side DataStore
  9. // (e.g. dojox.data.QueryReadStore), so we need to be sure what kind of
  10. // DataStore is being used
  11. serverStore: false, // server side store
  12. constructor: function(/* Object */ args){
  13. this.serverStore = !!args.serverStore;
  14. },
  15. mayHaveChildren: function(/*dojo.data.Item*/ item){
  16. var children = null;
  17. return array.some(this.childrenAttrs, function(attr){
  18. children = this.store.getValue(item, attr);
  19. if(lang.isString(children)){
  20. return parseInt(children, 10) > 0 || children.toLowerCase() === "true" ? true : false;
  21. }else if(typeof children == "number"){
  22. return children > 0;
  23. }else if(typeof children == "boolean"){
  24. return children;
  25. }else if(this.store.isItem(children)){
  26. children = this.store.getValues(item, attr);
  27. return lang.isArray(children) ? children.length > 0 : false;
  28. }else{
  29. return false;
  30. }
  31. }, this);
  32. },
  33. getChildren: function(/*dojo.data.Item*/parentItem, /*function(items, size)*/onComplete, /*function*/ onError, /*object*/queryObj){
  34. if(queryObj){
  35. var start = queryObj.start || 0,
  36. count = queryObj.count,
  37. parentId = queryObj.parentId,
  38. sort = queryObj.sort;
  39. if(parentItem === this.root){
  40. this.root.size = 0;
  41. this.store.fetch({
  42. start: start,
  43. count: count,
  44. sort: sort,
  45. query: this.query,
  46. onBegin: lang.hitch(this, function(size){
  47. this.root.size = size;
  48. }),
  49. onComplete: lang.hitch(this, function(items){
  50. onComplete(items, queryObj, this.root.size);
  51. }),
  52. onError: onError
  53. });
  54. }else{
  55. var store = this.store;
  56. if(!store.isItemLoaded(parentItem)){
  57. var getChildren = lang.hitch(this, arguments.callee);
  58. store.loadItem({
  59. item: parentItem,
  60. onItem: function(parentItem){
  61. getChildren(parentItem, onComplete, onError, queryObj);
  62. },
  63. onError: onError
  64. });
  65. return;
  66. }
  67. if(this.serverStore && !this._isChildrenLoaded(parentItem)){
  68. this.childrenSize = 0;
  69. this.store.fetch({
  70. start: start,
  71. count: count,
  72. sort: sort,
  73. query: lang.mixin({parentId: parentId}, this.query || {}),
  74. onBegin: lang.hitch(this, function(size){
  75. this.childrenSize = size;
  76. }),
  77. onComplete: lang.hitch(this, function(items){
  78. onComplete(items, queryObj, this.childrenSize);
  79. }),
  80. onError: onError
  81. });
  82. }else{
  83. this.inherited(arguments);
  84. }
  85. }
  86. }else{
  87. this.inherited(arguments);
  88. }
  89. },
  90. _isChildrenLoaded: function(parentItem){
  91. // summary:
  92. // Check if all children of the given item have been loaded
  93. var children = null;
  94. return array.every(this.childrenAttrs, function(attr){
  95. children = this.store.getValues(parentItem, attr);
  96. return array.every(children, function(c){
  97. return this.store.isItemLoaded(c);
  98. }, this);
  99. }, this);
  100. },
  101. //overwritten
  102. onNewItem: function(item, parentInfo){ },
  103. onDeleteItem: function(item){ }
  104. });
  105. });