diff --git a/example/store/bundler.js b/example/store/bundler.js index 7244a69f4..30568d78a 100644 --- a/example/store/bundler.js +++ b/example/store/bundler.js @@ -112,7 +112,6 @@ value: function add(evt) { evt.preventDefault(); this.store.add(); - this.update(); } }, { key: 'style', @@ -2706,6 +2705,46 @@ return handler(); }); } + }, { + key: "update", + value: function update() { + this._mergeInstances(); + this.instances.forEach(function (instance) { + return instance.update(); + }); + } + }, { + key: "_mergeInstances", + value: function _mergeInstances() { + var _this = this; + + var arr = []; + var idArr = []; + this.instances.forEach(function (instance) { + idArr.push(instance.id); + }); + + this.instances.forEach(function (instance) { + if (!instance.parent) { + arr.push(instance); + } else { + if (!_this._isSubInstance(instance, idArr)) { + arr.push(instance); + } + } + }); + + this.instances = arr; + } + }, { + key: "_isSubInstance", + value: function _isSubInstance(instance, arr) { + if (arr.indexOf(instance.parent.id) !== -1) { + return true; + } else if (instance.parent.parent) { + return this._isSubInstance(instance.parent, arr); + } + } }]); return Store; @@ -2843,6 +2882,7 @@ this.data.items.push(this.data.text); this.data.text = ""; this.data.length = this.data.items.length; + this.update(); } }, { key: "updateText", @@ -2854,6 +2894,7 @@ value: function clear() { this.data.items.length = 0; this.data.length = 0; + this.update(); } }]); diff --git a/example/store/todo-data.js b/example/store/todo-data.js index 143f06305..e1bb17229 100644 --- a/example/store/todo-data.js +++ b/example/store/todo-data.js @@ -11,6 +11,7 @@ class TodoData extends Omi.Store { this.data.items.push(this.data.text) this.data.text = "" this.data.length = this.data.items.length + this.update() } updateText(text){ @@ -20,6 +21,7 @@ class TodoData extends Omi.Store { clear(){ this.data.items.length = 0 this.data.length = 0 + this.update() } } diff --git a/example/store/todo.js b/example/store/todo.js index 992a4dbdf..9fb353bca 100644 --- a/example/store/todo.js +++ b/example/store/todo.js @@ -14,7 +14,6 @@ class Todo extends Omi.Component { add (evt) { evt.preventDefault(); this.store.add(); - this.update(); } style () { diff --git a/src/store.js b/src/store.js index 98a57e98a..1cbbe6898 100644 --- a/src/store.js +++ b/src/store.js @@ -5,20 +5,52 @@ class Store { this.instances = [] } - ready(readyHandler){ - if(this.isReady){ + ready(readyHandler) { + if (this.isReady) { readyHandler() return } this.readyHandlers.push(readyHandler) } - beReady(){ - this.isReady = true + beReady() { + this.isReady = true this.readyHandlers.forEach((handler)=>handler()) } + update() { + this._mergeInstances() + this.instances.forEach(instance=>instance.update()) + } + _mergeInstances(){ + let arr = [] + let idArr = [] + this.instances.forEach(instance=>{ + idArr.push(instance.id) + }) + + this.instances.forEach(instance=>{ + if(!instance.parent){ + arr.push(instance) + }else{ + if(!this._isSubInstance(instance,idArr)){ + arr.push(instance) + } + } + + }) + + this.instances = arr; + } + + _isSubInstance(instance,arr) { + if (arr.indexOf(instance.parent.id) !== -1) { + return true; + } else if(instance.parent.parent){ + return this._isSubInstance(instance.parent,arr) + } + } }