diff --git a/demo/js/config/fix.js b/demo/js/config/fix.js index f9645572e7..6080f514d7 100644 --- a/demo/js/config/fix.js +++ b/demo/js/config/fix.js @@ -91,6 +91,11 @@ Demo.FIX_CONFIG = [{ }, { id: 72, pId: 7, + text: "state属性", + value: "demo.fix6" +}, { + id: 78, + pId: 7, text: "计算属性", value: "demo.fix2" }, { diff --git a/demo/js/fix-2.0/state.js b/demo/js/fix-2.0/state.js new file mode 100644 index 0000000000..3151542592 --- /dev/null +++ b/demo/js/fix-2.0/state.js @@ -0,0 +1,49 @@ +;(function () { + var State = BI.inherit(Fix.VM, { + state: function () { + return { + name: "原始属性" + } + }, + computed: { + b: function () { + return this.model.name + "-计算属性" + } + } + }) + + Demo.Fix = BI.inherit(BI.Widget, { + _store: function () { + return new State(); + }, + watch: { + b: function () { + this.button.setText(this.model.b) + } + }, + render: function () { + var self = this; + return { + type: "bi.absolute", + items: [{ + el: { + type: "bi.button", + ref: function () { + self.button = this; + }, + handler: function () { + self.model.name = "这是改变后的属性" + }, + text: this.model.b + } + }] + } + }, + mounted: function () { + + + } + }); + + BI.shortcut("demo.fix6", Demo.Fix); +}()); \ No newline at end of file diff --git a/dist/demo.js b/dist/demo.js index a33417e721..2578308627 100644 --- a/dist/demo.js +++ b/dist/demo.js @@ -5257,6 +5257,11 @@ Demo.FIX_CONFIG = [{ }, { id: 72, pId: 7, + text: "state属性", + value: "demo.fix6" +}, { + id: 78, + pId: 7, text: "计算属性", value: "demo.fix2" }, { @@ -10640,7 +10645,55 @@ BI.shortcut("demo.tmp", Demo.Func); }); BI.shortcut("demo.fix_scene_fine_index_update", Demo.FixSceneFineIndexUpdate); -})();;(function(){ +})();;(function () { + var State = BI.inherit(Fix.VM, { + state: function () { + return { + name: "原始属性" + } + }, + computed: { + b: function () { + return this.model.name + "-计算属性" + } + } + }) + + Demo.Fix = BI.inherit(BI.Widget, { + _store: function () { + return new State(); + }, + watch: { + b: function () { + this.button.setText(this.model.b) + } + }, + render: function () { + var self = this; + return { + type: "bi.absolute", + items: [{ + el: { + type: "bi.button", + ref: function () { + self.button = this; + }, + handler: function () { + self.model.name = "这是改变后的属性" + }, + text: this.model.b + } + }] + } + }, + mounted: function () { + + + } + }); + + BI.shortcut("demo.fix6", Demo.Fix); +}());;(function(){ var model = Fix.define({ name: "原始属性", arr: [{ diff --git a/dist/fix/fix.js b/dist/fix/fix.js index 18bdf0af45..a8185b8406 100644 --- a/dist/fix/fix.js +++ b/dist/fix/fix.js @@ -820,6 +820,10 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons var computedWatcherOptions = { lazy: true }; + function initState(vm, state) { + vm.$$state = observe(state).model; + } + function initComputed(vm, computed) { var watchers = vm._computedWatchers = {}; @@ -894,7 +898,8 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons } else { this.options = model || {}; } - var keys = _.keys(this.$$model).concat(_.keys(this.computed)); + var state = this.state && this.state(); + var keys = _.keys(this.$$model).concat(_.keys(state)).concat(_.keys(this.computed)); var props = {}; var _loop = function _loop(i, len) { @@ -904,13 +909,21 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons enumerable: true, configurable: true, get: function get() { - return key in vm.$$computed ? vm.$$computed[key] : vm.$$model[key]; + if (key in vm.$$computed) { + return vm.$$computed[key]; + } + if (key in vm.$$state) { + return vm.$$state[key]; + } + return vm.$$model[key]; }, set: function set(val) { - if (!vm.$$model || !(key in vm.$$model)) { - return; + if (key in vm.$$state) { + return vm.$$state[key] = val; + } + if (key in vm.$$model) { + return vm.$$model[key] = val; } - return vm.$$model[key] = val; } }; } @@ -921,6 +934,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons } this.model = createViewModel$1({}, props); this.$$model && (this.model.__ob__ = this.$$model.__ob__); + state && initState(this, state); initComputed(this, this.computed); initMethods(this, this.actions); this._init(); @@ -1110,6 +1124,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons exports.define = define; exports.version = version; exports.$$skipArray = $$skipArray; + exports.initState = initState; exports.VM = VM; exports.observerState = observerState; exports.Observer = Observer;