behavior.js 878 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. export var behavior = Behavior({
  2. created: function created() {
  3. var _this = this;
  4. if (!this.$options) {
  5. return;
  6. }
  7. var cache = {};
  8. var _this$$options = this.$options(),
  9. computed = _this$$options.computed;
  10. var keys = Object.keys(computed);
  11. this.calcComputed = function () {
  12. var needUpdate = {};
  13. keys.forEach(function (key) {
  14. var value = computed[key].call(_this);
  15. if (cache[key] !== value) {
  16. cache[key] = needUpdate[key] = value;
  17. }
  18. });
  19. return needUpdate;
  20. };
  21. },
  22. attached: function attached() {
  23. this.set();
  24. },
  25. methods: {
  26. // set data and set computed data
  27. set: function set(data, callback) {
  28. if (data) {
  29. this.setData(data, callback);
  30. }
  31. if (this.calcComputed) {
  32. this.setData(this.calcComputed());
  33. }
  34. }
  35. }
  36. });