abstract_store.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * DevExtreme (data/abstract_store.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var Class = require("../core/class");
  11. var abstract = Class.abstract;
  12. var EventsMixin = require("../core/events_mixin");
  13. var each = require("../core/utils/iterator").each;
  14. var errorsModule = require("./errors");
  15. var dataUtils = require("./utils");
  16. var compileGetter = require("../core/utils/data").compileGetter;
  17. var storeHelper = require("./store_helper");
  18. var queryByOptions = storeHelper.queryByOptions;
  19. var Deferred = require("../core/utils/deferred").Deferred;
  20. var noop = require("../core/utils/common").noop;
  21. var storeImpl = {};
  22. var Store = Class.inherit({
  23. ctor: function(options) {
  24. var that = this;
  25. options = options || {};
  26. each(["onLoaded", "onLoading", "onInserted", "onInserting", "onUpdated", "onUpdating", "onPush", "onRemoved", "onRemoving", "onModified", "onModifying"], function(_, optionName) {
  27. if (optionName in options) {
  28. that.on(optionName.slice(2).toLowerCase(), options[optionName])
  29. }
  30. });
  31. this._key = options.key;
  32. this._errorHandler = options.errorHandler;
  33. this._useDefaultSearch = true
  34. },
  35. _customLoadOptions: function() {
  36. return null
  37. },
  38. key: function() {
  39. return this._key
  40. },
  41. keyOf: function(obj) {
  42. if (!this._keyGetter) {
  43. this._keyGetter = compileGetter(this.key())
  44. }
  45. return this._keyGetter(obj)
  46. },
  47. _requireKey: function() {
  48. if (!this.key()) {
  49. throw errorsModule.errors.Error("E4005")
  50. }
  51. },
  52. load: function(options) {
  53. var that = this;
  54. options = options || {};
  55. this.fireEvent("loading", [options]);
  56. return this._withLock(this._loadImpl(options)).done(function(result) {
  57. that.fireEvent("loaded", [result, options])
  58. })
  59. },
  60. _loadImpl: function(options) {
  61. return queryByOptions(this.createQuery(options), options).enumerate()
  62. },
  63. _withLock: function(task) {
  64. var result = new Deferred;
  65. task.done(function() {
  66. var that = this;
  67. var args = arguments;
  68. dataUtils.processRequestResultLock.promise().done(function() {
  69. result.resolveWith(that, args)
  70. })
  71. }).fail(function() {
  72. result.rejectWith(this, arguments)
  73. });
  74. return result
  75. },
  76. createQuery: abstract,
  77. totalCount: function(options) {
  78. return this._totalCountImpl(options)
  79. },
  80. _totalCountImpl: function(options) {
  81. return queryByOptions(this.createQuery(options), options, true).count()
  82. },
  83. byKey: function(key, extraOptions) {
  84. return this._addFailHandlers(this._withLock(this._byKeyImpl(key, extraOptions)))
  85. },
  86. _byKeyImpl: abstract,
  87. insert: function(values) {
  88. var that = this;
  89. that.fireEvent("modifying");
  90. that.fireEvent("inserting", [values]);
  91. return that._addFailHandlers(that._insertImpl(values).done(function(callbackValues, callbackKey) {
  92. that.fireEvent("inserted", [callbackValues, callbackKey]);
  93. that.fireEvent("modified")
  94. }))
  95. },
  96. _insertImpl: abstract,
  97. update: function(key, values) {
  98. var that = this;
  99. that.fireEvent("modifying");
  100. that.fireEvent("updating", [key, values]);
  101. return that._addFailHandlers(that._updateImpl(key, values).done(function() {
  102. that.fireEvent("updated", [key, values]);
  103. that.fireEvent("modified")
  104. }))
  105. },
  106. _updateImpl: abstract,
  107. push: function(changes) {
  108. this._pushImpl(changes);
  109. this.fireEvent("push", [changes])
  110. },
  111. _pushImpl: noop,
  112. remove: function(key) {
  113. var that = this;
  114. that.fireEvent("modifying");
  115. that.fireEvent("removing", [key]);
  116. return that._addFailHandlers(that._removeImpl(key).done(function(callbackKey) {
  117. that.fireEvent("removed", [callbackKey]);
  118. that.fireEvent("modified")
  119. }))
  120. },
  121. _removeImpl: abstract,
  122. _addFailHandlers: function(deferred) {
  123. return deferred.fail(this._errorHandler).fail(errorsModule._errorHandler)
  124. }
  125. }).include(EventsMixin);
  126. Store.create = function(alias, options) {
  127. if (!(alias in storeImpl)) {
  128. throw errorsModule.errors.Error("E4020", alias)
  129. }
  130. return new storeImpl[alias](options)
  131. };
  132. Store.registerClass = function(type, alias) {
  133. if (alias) {
  134. storeImpl[alias] = type
  135. }
  136. return type
  137. };
  138. Store.inherit = function(inheritor) {
  139. return function(members, alias) {
  140. var type = inheritor.apply(this, [members]);
  141. Store.registerClass(type, alias);
  142. return type
  143. }
  144. }(Store.inherit);
  145. module.exports = Store;