index.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. module.exports =
  2. /******/ (function(modules) { // webpackBootstrap
  3. /******/ // The module cache
  4. /******/ var installedModules = {};
  5. /******/
  6. /******/ // The require function
  7. /******/ function __webpack_require__(moduleId) {
  8. /******/
  9. /******/ // Check if module is in cache
  10. /******/ if(installedModules[moduleId]) {
  11. /******/ return installedModules[moduleId].exports;
  12. /******/ }
  13. /******/ // Create a new module (and put it into the cache)
  14. /******/ var module = installedModules[moduleId] = {
  15. /******/ i: moduleId,
  16. /******/ l: false,
  17. /******/ exports: {}
  18. /******/ };
  19. /******/
  20. /******/ // Execute the module function
  21. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  22. /******/
  23. /******/ // Flag the module as loaded
  24. /******/ module.l = true;
  25. /******/
  26. /******/ // Return the exports of the module
  27. /******/ return module.exports;
  28. /******/ }
  29. /******/
  30. /******/
  31. /******/ // expose the modules object (__webpack_modules__)
  32. /******/ __webpack_require__.m = modules;
  33. /******/
  34. /******/ // expose the module cache
  35. /******/ __webpack_require__.c = installedModules;
  36. /******/
  37. /******/ // define getter function for harmony exports
  38. /******/ __webpack_require__.d = function(exports, name, getter) {
  39. /******/ if(!__webpack_require__.o(exports, name)) {
  40. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  41. /******/ }
  42. /******/ };
  43. /******/
  44. /******/ // define __esModule on exports
  45. /******/ __webpack_require__.r = function(exports) {
  46. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  47. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  48. /******/ }
  49. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  50. /******/ };
  51. /******/
  52. /******/ // create a fake namespace object
  53. /******/ // mode & 1: value is a module id, require it
  54. /******/ // mode & 2: merge all properties of value into the ns
  55. /******/ // mode & 4: return value when already ns object
  56. /******/ // mode & 8|1: behave like require
  57. /******/ __webpack_require__.t = function(value, mode) {
  58. /******/ if(mode & 1) value = __webpack_require__(value);
  59. /******/ if(mode & 8) return value;
  60. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  61. /******/ var ns = Object.create(null);
  62. /******/ __webpack_require__.r(ns);
  63. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  64. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  65. /******/ return ns;
  66. /******/ };
  67. /******/
  68. /******/ // getDefaultExport function for compatibility with non-harmony modules
  69. /******/ __webpack_require__.n = function(module) {
  70. /******/ var getter = module && module.__esModule ?
  71. /******/ function getDefault() { return module['default']; } :
  72. /******/ function getModuleExports() { return module; };
  73. /******/ __webpack_require__.d(getter, 'a', getter);
  74. /******/ return getter;
  75. /******/ };
  76. /******/
  77. /******/ // Object.prototype.hasOwnProperty.call
  78. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  79. /******/
  80. /******/ // __webpack_public_path__
  81. /******/ __webpack_require__.p = "";
  82. /******/
  83. /******/
  84. /******/ // Load entry module and return exports
  85. /******/ return __webpack_require__(__webpack_require__.s = 0);
  86. /******/ })
  87. /************************************************************************/
  88. /******/ ([
  89. /* 0 */
  90. /***/ (function(module, exports, __webpack_require__) {
  91. "use strict";
  92. exports.__esModule = true;
  93. exports.storeBindingsBehavior = undefined;
  94. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  95. exports.createStoreBindings = createStoreBindings;
  96. var _mobxMiniprogram = __webpack_require__(1);
  97. function _createActions(methods, options) {
  98. var store = options.store,
  99. actions = options.actions;
  100. if (!actions) return;
  101. // for array-typed fields definition
  102. if (typeof store === 'undefined') {
  103. throw new Error('[mobx-miniprogram] no store specified');
  104. }
  105. if (actions instanceof Array) {
  106. // eslint-disable-next-line arrow-body-style
  107. actions.forEach(function (field) {
  108. methods[field] = function () {
  109. return store[field].apply(store, arguments);
  110. };
  111. });
  112. } else if ((typeof actions === 'undefined' ? 'undefined' : _typeof(actions)) === 'object') {
  113. // for object-typed fields definition
  114. Object.keys(actions).forEach(function (field) {
  115. var def = actions[field];
  116. if (typeof field !== 'string' && typeof field !== 'number') {
  117. throw new Error('[mobx-miniprogram] unrecognized field definition');
  118. }
  119. methods[field] = function () {
  120. return store[def].apply(store, arguments);
  121. };
  122. });
  123. }
  124. }
  125. function _createDataFieldsReactions(target, options) {
  126. var store = options.store,
  127. fields = options.fields;
  128. // setData combination
  129. var pendingSetData = null;
  130. function applySetData() {
  131. if (pendingSetData === null) return;
  132. var data = pendingSetData;
  133. pendingSetData = null;
  134. target.setData(data);
  135. }
  136. function scheduleSetData(field, value) {
  137. if (!pendingSetData) {
  138. pendingSetData = {};
  139. wx.nextTick(applySetData);
  140. }
  141. pendingSetData[field] = value;
  142. }
  143. // handling fields
  144. var reactions = [];
  145. if (fields instanceof Array) {
  146. // for array-typed fields definition
  147. if (typeof store === 'undefined') {
  148. throw new Error('[mobx-miniprogram] no store specified');
  149. }
  150. // eslint-disable-next-line arrow-body-style
  151. reactions = fields.map(function (field) {
  152. return (0, _mobxMiniprogram.reaction)(function () {
  153. return store[field];
  154. }, function (value) {
  155. scheduleSetData(field, value);
  156. }, {
  157. fireImmediately: true
  158. });
  159. });
  160. } else if ((typeof fields === 'undefined' ? 'undefined' : _typeof(fields)) === 'object' && fields) {
  161. // for object-typed fields definition
  162. reactions = Object.keys(fields).map(function (field) {
  163. var def = fields[field];
  164. if (typeof def === 'function') {
  165. return (0, _mobxMiniprogram.reaction)(function () {
  166. return def.call(target, store);
  167. }, function (value) {
  168. scheduleSetData(field, value);
  169. }, {
  170. fireImmediately: true
  171. });
  172. }
  173. if (typeof field !== 'string' && typeof field !== 'number') {
  174. throw new Error('[mobx-miniprogram] unrecognized field definition');
  175. }
  176. if (typeof store === 'undefined') {
  177. throw new Error('[mobx-miniprogram] no store specified');
  178. }
  179. return (0, _mobxMiniprogram.reaction)(function () {
  180. return store[def];
  181. }, function (value) {
  182. scheduleSetData(String(field), value);
  183. }, {
  184. fireImmediately: true
  185. });
  186. });
  187. }
  188. var destroyStoreBindings = function destroyStoreBindings() {
  189. reactions.forEach(function (reaction) {
  190. return reaction();
  191. });
  192. };
  193. return {
  194. updateStoreBindings: applySetData,
  195. destroyStoreBindings: destroyStoreBindings
  196. };
  197. }
  198. function createStoreBindings(target, options) {
  199. _createActions(target, options);
  200. return _createDataFieldsReactions(target, options);
  201. }
  202. var storeBindingsBehavior = exports.storeBindingsBehavior = Behavior({
  203. definitionFilter: function definitionFilter(defFields) {
  204. if (!defFields.methods) {
  205. defFields.methods = {};
  206. }
  207. var storeBindings = defFields.storeBindings;
  208. defFields.methods._mobxMiniprogramBindings = function () {
  209. return storeBindings;
  210. };
  211. if (storeBindings) {
  212. if (Array.isArray(storeBindings)) {
  213. storeBindings.forEach(function (binding) {
  214. _createActions(defFields.methods, binding);
  215. });
  216. } else {
  217. _createActions(defFields.methods, storeBindings);
  218. }
  219. }
  220. },
  221. attached: function attached() {
  222. if (typeof this._mobxMiniprogramBindings !== 'function') return;
  223. var storeBindings = this._mobxMiniprogramBindings();
  224. if (!storeBindings) {
  225. this._mobxMiniprogramBindings = null;
  226. return;
  227. }
  228. if (Array.isArray(storeBindings)) {
  229. var that = this;
  230. this._mobxMiniprogramBindings = storeBindings.map(function (item) {
  231. return _createDataFieldsReactions(that, item);
  232. });
  233. } else {
  234. this._mobxMiniprogramBindings = _createDataFieldsReactions(this, storeBindings);
  235. }
  236. },
  237. detached: function detached() {
  238. if (this._mobxMiniprogramBindings) {
  239. if (Array.isArray(this._mobxMiniprogramBindings)) {
  240. this._mobxMiniprogramBindings.forEach(function (bd) {
  241. bd.destroyStoreBindings();
  242. });
  243. } else {
  244. this._mobxMiniprogramBindings.destroyStoreBindings();
  245. }
  246. }
  247. },
  248. methods: {
  249. updateStoreBindings: function updateStoreBindings() {
  250. if (this._mobxMiniprogramBindings && typeof this._mobxMiniprogramBindings !== 'function') {
  251. if (Array.isArray(this._mobxMiniprogramBindings)) {
  252. this._mobxMiniprogramBindings.forEach(function (bd) {
  253. bd.updateStoreBindings();
  254. });
  255. } else {
  256. this._mobxMiniprogramBindings.updateStoreBindings();
  257. }
  258. }
  259. }
  260. }
  261. });
  262. /***/ }),
  263. /* 1 */
  264. /***/ (function(module, exports) {
  265. module.exports = require("mobx-miniprogram");
  266. /***/ })
  267. /******/ ]);
  268. //# sourceMappingURL=index.js.map