auto update view when store changed

This commit is contained in:
dntzhang 2017-03-17 17:26:54 +08:00
parent 9fd5f1b58b
commit ce73ecdb23
4 changed files with 80 additions and 6 deletions

View File

@ -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();
}
}]);

View File

@ -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()
}
}

View File

@ -14,7 +14,6 @@ class Todo extends Omi.Component {
add (evt) {
evt.preventDefault();
this.store.add();
this.update();
}
style () {

View File

@ -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)
}
}
}