Skip to content

Commit e7ad9c6

Browse files
committed
merge r-model
1 parent 8e28803 commit e7ad9c6

9 files changed

Lines changed: 124 additions & 84 deletions

File tree

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
![logo](http://regularjs.github.io//asserts/image/regular-icon-100.png)
1+
22

33
# regular = react(ractive) + angular.
44

@@ -24,10 +24,10 @@ __template__
2424

2525
```html
2626
{{#list notes as c}}
27-
<p>{{c.content}}<a href='#' on-click={{this.remove(c)}}>remove</a></p>
27+
&amp;amp;lt;p>{{c.content}}&amp;amp;lt;a href='#' on-click={{this.remove(c)}}>remove&amp;amp;lt;/a>&amp;amp;lt;/p>
2828
{{/list}}
29-
<textarea r-model={{draftComment}}></textarea>
30-
<button on-click={{this.add()}}>new Note</button>
29+
&amp;amp;lt;textarea r-model={{draftComment}}>&amp;amp;lt;/textarea>
30+
&amp;amp;lt;button on-click={{this.add()}}>new Note&amp;amp;lt;/button>
3131
```
3232

3333
__javascript__
@@ -59,7 +59,7 @@ then you can find `regular.js` and `regular.min.js` in `dist` folder;
5959
or....
6060

6161
```html
62-
<script src="https://rawgit.com/regularjs/regular/master/dist/regular.min.js"></script>
62+
&amp;amp;lt;script src="https://rawgit.com/regularjs/regular/master/dist/regular.min.js">&amp;amp;lt;/script>
6363
```
6464

6565

@@ -119,7 +119,7 @@ IE7+ and other modern browser. __In fact, most of our products need to support I
119119
1. `@(Expression)` to create binding-once Expression
120120
2. lightweight animation support
121121
3. svg support
122-
4. `{{#if }}` can be used in tag. like `<div {{#if !user }} on-click = {{this.login()}}{{/if}}></div>`
122+
4. `{{#if }}` can be used in tag. like `&amp;amp;lt;div {{#if !user }} on-click = {{this.login()}}{{/if}}>&amp;amp;lt;/div>`
123123

124124

125125
## TODO

dist/regular.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,19 @@ walkers.element = function(ast){
14701470

14711471
var child;
14721472

1473+
if(group && !_.isVoidTag(ast.tag)){
1474+
dom.inject( combine.node(group) , element)
1475+
}
1476+
1477+
// sort before
1478+
attrs.sort(function(a1, a2){
1479+
var d1 = Constructor.directive(a1.name),
1480+
d2 = Constructor.directive(a2.name);
1481+
if(d1 && d2) return (d2.priority || 1) - (d1.priority || 1);
1482+
if(d1) return 1;
1483+
if(d2) return -1;
1484+
return -1;
1485+
})
14731486
// may distinct with if else
14741487
var destroies = walkAttributes.call(this, attrs, element, destroies);
14751488

@@ -1480,10 +1493,6 @@ walkers.element = function(ast){
14801493
type: "element",
14811494
group: group,
14821495
node: function(){
1483-
if(!res.init && group && !_.isVoidTag(ast.tag)){
1484-
animate.inject( combine.node(group) , element)
1485-
res.init = true;
1486-
}
14871496
return element;
14881497
},
14891498
last: function(){
@@ -3318,6 +3327,7 @@ var methods = {
33183327
get = expr.get;
33193328
once = expr.once || expr.constant;
33203329
}
3330+
33213331
this._watchers.push({
33223332
id: uid,
33233333
get: get,
@@ -3996,12 +4006,10 @@ function initSelect( elem, parsed){
39964006
self.$update();
39974007
inProgress = false;
39984008
}
3999-
dom.on(elem, "change", handler);
4000-
this.$on('init', function(){
4001-
if(parsed.get(self) === undefined){
4002-
parsed.set(self, elem.value);
4003-
}
4004-
});
4009+
4010+
if(parsed.get(self) === undefined && elem.value){
4011+
parsed.set(self, elem.value);
4012+
}
40054013
return function destroy(){
40064014
dom.off(elem, "change", handler);
40074015
}
@@ -4044,11 +4052,9 @@ function initText(elem, parsed){
40444052
dom.on(elem, "cut", handler)
40454053
dom.on(elem, "change", handler)
40464054
}
4047-
this.$on('init', function(){
4048-
if(parsed.get(self) === undefined){
4049-
parsed.set(self, elem.value);
4050-
}
4051-
})
4055+
if(parsed.get(self) === undefined && elem.value){
4056+
parsed.set(self, elem.value);
4057+
}
40524058
return function destroy(){
40534059
if(dom.msie !== 9 && "oninput" in dom.tNode ){
40544060
elem.removeEventListener("input", handler );
@@ -4080,12 +4086,10 @@ function initCheckBox(elem, parsed){
40804086
inProgress = false;
40814087
}
40824088
if(parsed.set) dom.on(elem, "change", handler)
4083-
this.$on('init', function(){
40844089

4085-
if(parsed.get(self) === undefined){
4086-
parsed.set(self, elem.checked);
4087-
}
4088-
});
4090+
if(parsed.get(self) === undefined){
4091+
parsed.set(self, !!elem.checked);
4092+
}
40894093

40904094
return function destroy(){
40914095
if(parsed.set) dom.off(elem, "change", handler)
@@ -4113,11 +4117,9 @@ function initRadio(elem, parsed){
41134117
}
41144118
if(parsed.set) dom.on(elem, "change", handler)
41154119
// beacuse only after compile(init), the dom structrue is exsit.
4116-
this.$on('init', function(){
4117-
if(parsed.get(self) === undefined){
4118-
if(elem.checked) parsed.set(self, elem.value);
4119-
}
4120-
});
4120+
if(parsed.get(self) === undefined){
4121+
if(elem.checked) parsed.set(self, elem.value);
4122+
}
41214123

41224124
return function destroy(){
41234125
if(parsed.set) dom.off(elem, "change", handler)

dist/regular.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/directive/form.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,10 @@ function initSelect( elem, parsed){
5353
self.$update();
5454
inProgress = false;
5555
}
56-
dom.on(elem, "change", handler);
57-
this.$on('init', function(){
58-
if(parsed.get(self) === undefined){
59-
parsed.set(self, elem.value);
60-
}
61-
});
56+
57+
if(parsed.get(self) === undefined && elem.value){
58+
parsed.set(self, elem.value);
59+
}
6260
return function destroy(){
6361
dom.off(elem, "change", handler);
6462
}
@@ -101,11 +99,9 @@ function initText(elem, parsed){
10199
dom.on(elem, "cut", handler)
102100
dom.on(elem, "change", handler)
103101
}
104-
this.$on('init', function(){
105-
if(parsed.get(self) === undefined){
106-
parsed.set(self, elem.value);
107-
}
108-
})
102+
if(parsed.get(self) === undefined && elem.value){
103+
parsed.set(self, elem.value);
104+
}
109105
return function destroy(){
110106
if(dom.msie !== 9 && "oninput" in dom.tNode ){
111107
elem.removeEventListener("input", handler );
@@ -137,12 +133,10 @@ function initCheckBox(elem, parsed){
137133
inProgress = false;
138134
}
139135
if(parsed.set) dom.on(elem, "change", handler)
140-
this.$on('init', function(){
141136

142-
if(parsed.get(self) === undefined){
143-
parsed.set(self, elem.checked);
144-
}
145-
});
137+
if(parsed.get(self) === undefined){
138+
parsed.set(self, !!elem.checked);
139+
}
146140

147141
return function destroy(){
148142
if(parsed.set) dom.off(elem, "change", handler)
@@ -170,11 +164,9 @@ function initRadio(elem, parsed){
170164
}
171165
if(parsed.set) dom.on(elem, "change", handler)
172166
// beacuse only after compile(init), the dom structrue is exsit.
173-
this.$on('init', function(){
174-
if(parsed.get(self) === undefined){
175-
if(elem.checked) parsed.set(self, elem.value);
176-
}
177-
});
167+
if(parsed.get(self) === undefined){
168+
if(elem.checked) parsed.set(self, elem.value);
169+
}
178170

179171
return function destroy(){
180172
if(parsed.set) dom.off(elem, "change", handler)

src/helper/watcher.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var methods = {
3535
get = expr.get;
3636
once = expr.once || expr.constant;
3737
}
38+
3839
this._watchers.push({
3940
id: uid,
4041
get: get,

src/walkers.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,19 @@ walkers.element = function(ast){
242242

243243
var child;
244244

245+
if(group && !_.isVoidTag(ast.tag)){
246+
dom.inject( combine.node(group) , element)
247+
}
248+
249+
// sort before
250+
attrs.sort(function(a1, a2){
251+
var d1 = Constructor.directive(a1.name),
252+
d2 = Constructor.directive(a2.name);
253+
if(d1 && d2) return (d2.priority || 1) - (d1.priority || 1);
254+
if(d1) return 1;
255+
if(d2) return -1;
256+
return -1;
257+
})
245258
// may distinct with if else
246259
var destroies = walkAttributes.call(this, attrs, element, destroies);
247260

@@ -252,10 +265,6 @@ walkers.element = function(ast){
252265
type: "element",
253266
group: group,
254267
node: function(){
255-
if(!res.init && group && !_.isVoidTag(ast.tag)){
256-
animate.inject( combine.node(group) , element)
257-
res.init = true;
258-
}
259268
return element;
260269
},
261270
last: function(){

test/regular.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,19 @@ walkers.element = function(ast){
14681468

14691469
var child;
14701470

1471+
if(group && !_.isVoidTag(ast.tag)){
1472+
dom.inject( combine.node(group) , element)
1473+
}
1474+
1475+
// sort before
1476+
attrs.sort(function(a1, a2){
1477+
var d1 = Constructor.directive(a1.name),
1478+
d2 = Constructor.directive(a2.name);
1479+
if(d1 && d2) return (d2.priority || 1) - (d1.priority || 1);
1480+
if(d1) return 1;
1481+
if(d2) return -1;
1482+
return -1;
1483+
})
14711484
// may distinct with if else
14721485
var destroies = walkAttributes.call(this, attrs, element, destroies);
14731486

@@ -1478,10 +1491,6 @@ walkers.element = function(ast){
14781491
type: "element",
14791492
group: group,
14801493
node: function(){
1481-
if(!res.init && group && !_.isVoidTag(ast.tag)){
1482-
animate.inject( combine.node(group) , element)
1483-
res.init = true;
1484-
}
14851494
return element;
14861495
},
14871496
last: function(){
@@ -3316,6 +3325,7 @@ var methods = {
33163325
get = expr.get;
33173326
once = expr.once || expr.constant;
33183327
}
3328+
33193329
this._watchers.push({
33203330
id: uid,
33213331
get: get,
@@ -3994,12 +4004,10 @@ function initSelect( elem, parsed){
39944004
self.$update();
39954005
inProgress = false;
39964006
}
3997-
dom.on(elem, "change", handler);
3998-
this.$on('init', function(){
3999-
if(parsed.get(self) === undefined){
4000-
parsed.set(self, elem.value);
4001-
}
4002-
});
4007+
4008+
if(parsed.get(self) === undefined && elem.value){
4009+
parsed.set(self, elem.value);
4010+
}
40034011
return function destroy(){
40044012
dom.off(elem, "change", handler);
40054013
}
@@ -4042,11 +4050,9 @@ function initText(elem, parsed){
40424050
dom.on(elem, "cut", handler)
40434051
dom.on(elem, "change", handler)
40444052
}
4045-
this.$on('init', function(){
4046-
if(parsed.get(self) === undefined){
4047-
parsed.set(self, elem.value);
4048-
}
4049-
})
4053+
if(parsed.get(self) === undefined && elem.value){
4054+
parsed.set(self, elem.value);
4055+
}
40504056
return function destroy(){
40514057
if(dom.msie !== 9 && "oninput" in dom.tNode ){
40524058
elem.removeEventListener("input", handler );
@@ -4078,12 +4084,10 @@ function initCheckBox(elem, parsed){
40784084
inProgress = false;
40794085
}
40804086
if(parsed.set) dom.on(elem, "change", handler)
4081-
this.$on('init', function(){
40824087

4083-
if(parsed.get(self) === undefined){
4084-
parsed.set(self, elem.checked);
4085-
}
4086-
});
4088+
if(parsed.get(self) === undefined){
4089+
parsed.set(self, !!elem.checked);
4090+
}
40874091

40884092
return function destroy(){
40894093
if(parsed.set) dom.off(elem, "change", handler)
@@ -4111,11 +4115,9 @@ function initRadio(elem, parsed){
41114115
}
41124116
if(parsed.set) dom.on(elem, "change", handler)
41134117
// beacuse only after compile(init), the dom structrue is exsit.
4114-
this.$on('init', function(){
4115-
if(parsed.get(self) === undefined){
4116-
if(elem.checked) parsed.set(self, elem.value);
4117-
}
4118-
});
4118+
if(parsed.get(self) === undefined){
4119+
if(elem.checked) parsed.set(self, elem.value);
4120+
}
41194121

41204122
return function destroy(){
41214123
if(parsed.set) dom.off(elem, "change", handler)

test/spec/browser-directive.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ describe('r-model directive', function(){
193193
describe('checkbox binding', function(){
194194
it('input:checkbox"s initial state should be correct', function(){
195195
var template =
196-
"<input type='checkbox' r-model={{nontype}} checked >"+
196+
"<input checked type='checkbox' r-model={{nontype}} >"+
197197
"<input type='checkbox' r-model={{nontype3}}>"+
198198
"<input type='checkbox' r-model={{nontype2}} checked=checked>";
199199
var component = new Regular({
@@ -363,6 +363,38 @@ describe('r-model directive', function(){
363363
destroy(component, container);
364364

365365
})
366+
367+
it("r-model in if should works as expect", function(){
368+
var container = document.createElement('div')
369+
var Component = Regular.extend({});
370+
var component = new Regular({
371+
data: {test: true} ,
372+
template: "{{#if !test}}<input r-model={{item}} value='1' />{{/if}}"
373+
}).$inject(container)
374+
375+
component.$update("test", false);
376+
377+
expect(nes.one("input", container).value).to.equal('1');
378+
expect(component.data.item).to.equal('1');
379+
380+
destroy(component, container);
381+
382+
})
383+
it("r-model in list should works as expect", function(){
384+
var container = document.createElement('div')
385+
var Component = Regular.extend({});
386+
var component = new Regular({
387+
data: {test: true, hello: {}} ,
388+
template: "{{#list [1,2,3,4] as item}}<input r-model={{hello.name}} value='1' />{{/list}}"
389+
}).$inject(container)
390+
391+
expect(nes.one("input", container).value).to.equal('1');
392+
expect(component.data.hello.name).to.equal('1');
393+
394+
destroy(component, container);
395+
396+
})
397+
366398
})
367399

368400
})

0 commit comments

Comments
 (0)