mvvm.js 681 B

1234567891011121314151617181920212223242526272829303132333435
  1. function MVVM(options){
  2. this.$options = options;
  3. var data = this._data = this.$options.data;
  4. var me = this;
  5. // 数据代理
  6. // 实现 vm.xxx -> vm._data.xxx
  7. Object.keys(data).forEach(function(key) {
  8. me._proxy(key);
  9. });
  10. observe(data, this);
  11. this.$compile = new Compile(options.el || document.body, this)
  12. }
  13. MVVM.prototype = {
  14. $watch: function(key, cb, options) {
  15. new Watcher(this, key, cb);
  16. },
  17. _proxy: function(key) {
  18. var me = this;
  19. Object.defineProperty(me, key, {
  20. configurable: true,
  21. enumerable: true,
  22. get: function proxyGetter() {
  23. return me._data[key];
  24. },
  25. set: function proxySetter(newVal) {
  26. me._data[key] = newVal;
  27. }
  28. });
  29. }
  30. };