Skip to content

Commit

Permalink
对for指令的diff, patch进行优化
Browse files Browse the repository at this point in the history
  • Loading branch information
RubyLouvre committed May 20, 2016
1 parent 74f924a commit 9229afb
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 30 deletions.
18 changes: 14 additions & 4 deletions src/avalon.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@

//
//var avalon = require('./seed/modern')
//
//require('./filters/index')
//require('./vdom/index')
//require('./dom/modern')
//require('./directives/modern')
//require('./strategy/index')
//require('./component/index')
//require('./vmodel/modern')

var avalon = require('./seed/modern')
var avalon = require('./seed/compact') //这个版本兼容IE6

require('./filters/index')
require('./vdom/index')
require('./dom/modern')
require('./directives/modern')
require('./dom/compact')
require('./directives/compact')
require('./strategy/index')
require('./component/index')
require('./vmodel/modern')
require('./vmodel/compact')


require('../components/button/index')
Expand Down
8 changes: 3 additions & 5 deletions src/directives/attr.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

var attrUpdate = require('../dom/attr/compact')
var update = require('./_update')

avalon.directive('attr', {
parse: function (binding, num) {
Expand Down Expand Up @@ -28,11 +29,8 @@ avalon.directive('attr', {
cur.changeAttr = patch
}
}
if (cur.changeAttr) {
var list = cur.change || (cur.change = [])
if(avalon.Array.ensure(list, this.update)){
steps.count += 1
}
if (cur.changeAttr) {
update(cur, this.update, steps, 'attr' )
}
} else {
cur.props[name] = p
Expand Down
6 changes: 2 additions & 4 deletions src/directives/attr.modern.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

var attrUpdate = require('../dom/attr/modern')
var update = require('./_update')

avalon.directive('attr', {
parse: function (binding, num) {
Expand Down Expand Up @@ -29,10 +30,7 @@ avalon.directive('attr', {
}
}
if (cur.changeAttr) {
var list = cur.change || (cur.change = [])
if(avalon.Array.ensure(list, this.update)){
steps.count += 1
}
update(cur, attrUpdate, steps, 'attr' )
}
} else {
cur.props[name] = p
Expand Down
2 changes: 1 addition & 1 deletion src/directives/compact.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require('./important')
require('./controller2')
require('./controller')
//处理属性样式
require('./attr')
require('./css')
Expand Down
8 changes: 4 additions & 4 deletions src/directives/controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// 抽离出来公用
var update = require('./_update')

avalon.skipController = function (fast, vm, iv) {
if (fast) {
var id = vm.$render ? vm.$render.$id : vm.$id
Expand Down Expand Up @@ -37,10 +39,8 @@ avalon.directive('controller', {
},
diff: function (cur, pre, steps, name) {
if (pre.props[name] !== cur.props[name]) {
var list = cur.change || (cur.change = [])
if (avalon.Array.ensure(list, this.update)) {
steps.count += 1
}
console.log('controller',steps)
update(cur, this.update, steps, 'controller' )
}
},
update: function (node, vnode) {
Expand Down
8 changes: 3 additions & 5 deletions src/directives/css.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

var update = require('./_update')

avalon.directive('css', {
parse: function (binding, num) {
Expand All @@ -7,7 +8,7 @@ avalon.directive('css', {
diff: function (cur, pre, steps, name) {
var a = cur.props[name]
var p = pre.props[name]
if ( Object(a) === a) {
if (Object(a) === a) {
if (Array.isArray(a)) {
a = cur.props[name] = avalon.mix.apply({}, a)
}
Expand All @@ -27,10 +28,7 @@ avalon.directive('css', {
}
}
if (cur.changeStyle) {
var list = cur.change || (cur.change = [])
if(avalon.Array.ensure(list, this.update)){
steps.count += 1
}
update(cur, this.update, steps, 'css')
}
} else {
cur.props[name] = p
Expand Down
2 changes: 1 addition & 1 deletion src/seed/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ var plugins = {
}
kernel.plugins = plugins
kernel.plugins['interpolate'](['{{', '}}'])

//kernel.showDiff = true
kernel.debug = true
11 changes: 8 additions & 3 deletions src/strategy/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ function diff(current, previous, steps) {
}
break
case 8:
if (cur.directive === 'for') {
i = directives['for'].diff(current, previous, steps, i)
} else if (cur.directive) {//if widget
if (cur.directive === 'for' ) {
if(cur.hasChange){
i = directives['for'].diff(current, previous, steps, i)
}else{
avalon.shadowCopy(cur, previous[i])
delete cur.hasChange
}
} else if (cur.directive ) {//if widget
directives[cur.directive].diff(cur, pre, steps)
}
break
Expand Down
4 changes: 1 addition & 3 deletions src/strategy/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function patch(nodes, vnodes, parent, steps) {
next = node.nextSibling
}
if (vnode.directive === 'for') {
if (vnode.change) {
if (vnode.hasChange) {
if (!node) {
return
}
Expand All @@ -42,9 +42,7 @@ function patch(nodes, vnodes, parent, steps) {
}
next = vnode.endRepeat.nextSibling
}

}

//ms-for, ms-if, ms-widget会返回false
if (false === execHooks(node, vnode, parent, steps, 'change')) {
if (vnode.repeatCount) {
Expand Down

0 comments on commit 9229afb

Please sign in to comment.