LazyTreeGridStoreModel.js 3.5 KB

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