behavior.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. function setAsync(context, data) {
  4. return new Promise(function (resolve) {
  5. context.setData(data, resolve);
  6. });
  7. }
  8. exports.behavior = Behavior({
  9. created: function () {
  10. var _this = this;
  11. if (!this.$options) {
  12. return;
  13. }
  14. var cache = {};
  15. var computed = this.$options().computed;
  16. var keys = Object.keys(computed);
  17. this.calcComputed = function () {
  18. var needUpdate = {};
  19. keys.forEach(function (key) {
  20. var value = computed[key].call(_this);
  21. if (cache[key] !== value) {
  22. cache[key] = value;
  23. needUpdate[key] = value;
  24. }
  25. });
  26. return needUpdate;
  27. };
  28. },
  29. attached: function () {
  30. this.set();
  31. },
  32. methods: {
  33. // set data and set computed data
  34. set: function (data, callback) {
  35. var _this = this;
  36. var stack = [];
  37. if (data) {
  38. stack.push(setAsync(this, data));
  39. }
  40. if (this.calcComputed) {
  41. stack.push(setAsync(this, this.calcComputed()));
  42. }
  43. return Promise.all(stack).then(function (res) {
  44. if (callback && typeof callback === 'function') {
  45. callback.call(_this);
  46. }
  47. return res;
  48. });
  49. }
  50. }
  51. });