Skip to content

Commit

Permalink
fix isolate related issue
Browse files Browse the repository at this point in the history
  • Loading branch information
leeluolee committed Sep 29, 2015
1 parent 32ce0fe commit ef3b0a4
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 34 deletions.
33 changes: 27 additions & 6 deletions dist/regular.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
@author leeluolee
@version 0.3.1
@version 0.4.0
@homepage http://regularjs.github.io
*/
;(function(){
Expand Down Expand Up @@ -1642,7 +1642,7 @@ walkers.component = function(ast, options){
var definition = {
data: data,
events: events,
$parent: this,
$parent: (isolate & 2)? null: this,
$root: this.$root,
$outer: options.outer,
_body: ast.children
Expand Down Expand Up @@ -1670,7 +1670,9 @@ walkers.component = function(ast, options){
value = self._touchExpr(value);
// use bit operate to control scope
if( !(isolate & 2) )
this.$watch(value, component.$update.bind(component, name))
this.$watch(value, (function(name, val){
this.data[name] = val;
}).bind(component, name))
if( value.set && !(isolate & 1 ) )
// sync the data. it force the component don't trigger attr.name's first dirty echeck
component.$watch(name, self.$update.bind(self, value), {sync: true});
Expand Down Expand Up @@ -1867,6 +1869,7 @@ dom.find = function(sl){
if(sl.indexOf('#')!==-1) return document.getElementById( sl.slice(1) );
}


dom.inject = function(node, refer, position){

position = position || 'bottom';
Expand Down Expand Up @@ -3661,8 +3664,7 @@ var methods = {
},
// private digest logic
_digest: function(){
// if(this.context) return this.context.$digest();
// if(this.$emit) this.$emit('digest');

var watchers = this._watchers;
var dirty = false, children, watcher, watcherDirty;
if(watchers && watchers.length){
Expand All @@ -3676,7 +3678,9 @@ var methods = {
children = this._children;
if(children && children.length){
for(var m = 0, mlen = children.length; m < mlen; m++){
if(children[m] && children[m]._digest()) dirty = true;
var child = children[m];

if(child && child._digest()) dirty = true;
}
}
return dirty;
Expand Down Expand Up @@ -4218,6 +4222,23 @@ var combine = module.exports = {
}

}


// @TODO: need move to dom.js
dom.element = function( component, all ){
if(!component) return !all? null: [];
var nodes = combine.node( component );
if( nodes.nodeType === 1 ) return all? [nodes]: nodes;
var elements = [];
for(var i = 0; i<nodes.length ;i++){
var node = nodes[i];
if( node && node.nodeType === 1){
if(!all) return node;
elements.push(node);
}
}
return elements
}
});
require.register("regularjs/src/helper/arrayDiff.js", function(exports, require, module){

Expand Down
4 changes: 2 additions & 2 deletions dist/regular.min.js

Large diffs are not rendered by default.

15 changes: 2 additions & 13 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ try{

require('./scripts/release')(gulp);

gulp.task('default', ['test'], function() {});
gulp.task('default', ['watch'], function() {});


//one could also externalize common config into a separate file,
Expand Down Expand Up @@ -142,19 +142,8 @@ gulp.task('v', function(fn){


// watch file then build
gulp.task('dev', ['build'], function(){
gulp.task('watch', ['build'], function(){
gulp.watch(['component.json', 'src/**/*.js'], ['build'])
// var puer = spawn('puer', ["--no-reload"], {})
// puer.stdout.on('data', function (data) {
// console.log(""+ data);
// });
// puer.stderr.on('data', function (data) {
// console.log('stderr: ' + data);
// });

// puer.on('close', function (code) {
// console.log('puer test compelete!');
// });

})

Expand Down
1 change: 1 addition & 0 deletions src/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ dom.find = function(sl){
if(sl.indexOf('#')!==-1) return document.getElementById( sl.slice(1) );
}


dom.inject = function(node, refer, position){

position = position || 'bottom';
Expand Down
17 changes: 17 additions & 0 deletions src/helper/combine.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,21 @@ var combine = module.exports = {
}
}

}


// @TODO: need move to dom.js
dom.element = function( component, all ){
if(!component) return !all? null: [];
var nodes = combine.node( component );
if( nodes.nodeType === 1 ) return all? [nodes]: nodes;
var elements = [];
for(var i = 0; i<nodes.length ;i++){
var node = nodes[i];
if( node && node.nodeType === 1){
if(!all) return node;
elements.push(node);
}
}
return elements
}
7 changes: 4 additions & 3 deletions src/helper/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ var methods = {
},
// private digest logic
_digest: function(){
// if(this.context) return this.context.$digest();
// if(this.$emit) this.$emit('digest');

var watchers = this._watchers;
var dirty = false, children, watcher, watcherDirty;
if(watchers && watchers.length){
Expand All @@ -125,7 +124,9 @@ var methods = {
children = this._children;
if(children && children.length){
for(var m = 0, mlen = children.length; m < mlen; m++){
if(children[m] && children[m]._digest()) dirty = true;
var child = children[m];

if(child && child._digest()) dirty = true;
}
}
return dirty;
Expand Down
6 changes: 4 additions & 2 deletions src/walkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ walkers.component = function(ast, options){
var definition = {
data: data,
events: events,
$parent: this,
$parent: (isolate & 2)? null: this,
$root: this.$root,
$outer: options.outer,
_body: ast.children
Expand Down Expand Up @@ -464,7 +464,9 @@ walkers.component = function(ast, options){
value = self._touchExpr(value);
// use bit operate to control scope
if( !(isolate & 2) )
this.$watch(value, component.$update.bind(component, name))
this.$watch(value, (function(name, val){
this.data[name] = val;
}).bind(component, name))
if( value.set && !(isolate & 1 ) )
// sync the data. it force the component don't trigger attr.name's first dirty echeck
component.$watch(name, self.$update.bind(self, value), {sync: true});
Expand Down
33 changes: 27 additions & 6 deletions test/regular.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
@author leeluolee
@version 0.3.1
@version 0.4.0
@homepage http://regularjs.github.io
*/

Expand Down Expand Up @@ -1640,7 +1640,7 @@ walkers.component = function(ast, options){
var definition = {
data: data,
events: events,
$parent: this,
$parent: (isolate & 2)? null: this,
$root: this.$root,
$outer: options.outer,
_body: ast.children
Expand Down Expand Up @@ -1668,7 +1668,9 @@ walkers.component = function(ast, options){
value = self._touchExpr(value);
// use bit operate to control scope
if( !(isolate & 2) )
this.$watch(value, component.$update.bind(component, name))
this.$watch(value, (function(name, val){
this.data[name] = val;
}).bind(component, name))
if( value.set && !(isolate & 1 ) )
// sync the data. it force the component don't trigger attr.name's first dirty echeck
component.$watch(name, self.$update.bind(self, value), {sync: true});
Expand Down Expand Up @@ -1865,6 +1867,7 @@ dom.find = function(sl){
if(sl.indexOf('#')!==-1) return document.getElementById( sl.slice(1) );
}


dom.inject = function(node, refer, position){

position = position || 'bottom';
Expand Down Expand Up @@ -3659,8 +3662,7 @@ var methods = {
},
// private digest logic
_digest: function(){
// if(this.context) return this.context.$digest();
// if(this.$emit) this.$emit('digest');

var watchers = this._watchers;
var dirty = false, children, watcher, watcherDirty;
if(watchers && watchers.length){
Expand All @@ -3674,7 +3676,9 @@ var methods = {
children = this._children;
if(children && children.length){
for(var m = 0, mlen = children.length; m < mlen; m++){
if(children[m] && children[m]._digest()) dirty = true;
var child = children[m];

if(child && child._digest()) dirty = true;
}
}
return dirty;
Expand Down Expand Up @@ -4216,6 +4220,23 @@ var combine = module.exports = {
}

}


// @TODO: need move to dom.js
dom.element = function( component, all ){
if(!component) return !all? null: [];
var nodes = combine.node( component );
if( nodes.nodeType === 1 ) return all? [nodes]: nodes;
var elements = [];
for(var i = 0; i<nodes.length ;i++){
var node = nodes[i];
if( node && node.nodeType === 1){
if(!all) return node;
elements.push(node);
}
}
return elements
}
});
require.register("regularjs/src/helper/arrayDiff.js", function(exports, require, module){

Expand Down
4 changes: 2 additions & 2 deletions test/spec/browser-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ describe("refs attribute", function(){
expect(component.$refs.haha.id).to.equal('100')
component.$update('name', 'hehe')
expect(component.$refs.hehe.id).to.equal('100')
expect(component.$refs.haha==null).to.true
expect(component.$refs.haha==null).to.equal(true);

expect(component.$refs.hehe1.innerHTML).to.equal('1')
expect(component.$refs.hehe2.innerHTML).to.equal('2')
Expand Down Expand Up @@ -707,7 +707,7 @@ describe("refs attribute", function(){
expect(component.$refs.haha.data.id).to.equal('100')
component.$update('name', 'hehe')
expect(component.$refs.hehe.data.id).to.equal('100')
expect(component.$refs.haha==null).to.true
expect(component.$refs.haha==null).to.equal(true);

expect(component.$refs.hehe1.data.value).to.equal(1)
expect(component.$refs.hehe2.data.value).to.equal(2)
Expand Down
35 changes: 35 additions & 0 deletions test/spec/browser-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,41 @@ void function(){
})
})

describe("dom.element should work as expect", function(){
var Nested = Regular.extend({
name: "nest",
template: "{#if !show}<div id='hide'>HIDE</div>{/if}"
})
var component = new Regular({
template: "<nest show={show}></nest>{#if show}<div id='show'>SHOW</div>{/if}\
{#list nodes as node}<div class='list'>list{node_index}</div>{/list}",
data: {
show: false,
nodes: [1,2,3]
}
})
it("dom.element basic", function(){
component.$update('show', false);
expect(dom.element(component).innerHTML).to.equal('HIDE')
var all = dom.element(component, true);
expect(all.length).to.equal(4)
expect(all[1].className).to.equal('list')
expect(all[2].innerHTML).to.equal('list1')
})
it("dom.element is changed with its content", function(){
component.data.nodes = [1,2]
component.$update('show', true)
expect(dom.element(component).innerHTML).to.equal('SHOW')
var all = dom.element(component, true);
expect(all.length).to.equal(3)
expect(all[1].innerHTML).to.equal('list0')
expect(all[2].innerHTML).to.equal('list1')
})

})




})

Expand Down
36 changes: 36 additions & 0 deletions test/spec/browser-nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,42 @@ void function(){
destroy(component, containerAll);
})

it("component with (isolate &2) should stop digest phase from parent", function(){
var Test = NameSpace.extend({
name: 'nested7',
template: "<p>{title}</p>",
config: function(){
this.num = 0;
},
_digest: function(){
this.num++;
return this.supr();
}

})
var component = new NameSpace({
template: "<span>{title}</span>\
<nested7 ref=a title={title} isolate=3></nested7>\
<nested7 ref=b title={title} isolate=2></nested7>\
<nested7 ref=c title={title} isolate=1></nested7>\
<nested7 ref=d title={title} ></nested7>",
data: {title: 'leeluolee'}
});

expect(component.$refs.a.num).to.equal(2);
expect(component.$refs.b.num).to.equal(2);
expect(component.$refs.c.num).to.equal(2);
expect(component.$refs.d.num).to.equal(2);

component.$update('title', 'hello')

expect(component.$refs.a.num).to.equal(2);
expect(component.$refs.b.num).to.equal(2);
expect(component.$refs.c.num).to.equal(4);
expect(component.$refs.d.num).to.equal(4);

})



it("ab-cd-ef should convert to abCdEf when passed to nested component", function(){
Expand Down

0 comments on commit ef3b0a4

Please sign in to comment.