Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit 278948b

Browse files
committed
+ array expression test case
1 parent a47e7c8 commit 278948b

File tree

7 files changed

+194
-44
lines changed

7 files changed

+194
-44
lines changed

demo/demo.bisheng.tmp.html

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,9 @@
2929
</script>
3030

3131
<script id="hello" type="template">
32-
<form>
33-
<div class="radio">
34-
<label>
35-
<input type="radio" name="optionsRadios" value="{{radioValue1}}" checked="{{radioChecked1}}">
36-
Option one is this and that&mdash;be sure to include why it's great
37-
</label>
38-
</div>
39-
<div class="radio">
40-
<label>
41-
<input type="radio" name="optionsRadios" value="{{radioValue2}}" checked="{{radioChecked2}}">
42-
Option two can be something else and selecting it will deselect option one
43-
</label>
44-
</div>
45-
<p>
46-
{{optionsRadios}}
47-
</p>
48-
</form>
32+
{{#each list}}
33+
<p>{{array}}</p>
34+
{{/each}}
4935
</script>
5036

5137
<div class="container">
@@ -59,18 +45,21 @@
5945
// BS.auto(true)
6046
var tpl = $('#hello').html()
6147
data = {
62-
optionsRadios: 'optionsRadios',
63-
radioValue1: 'radioValue1',
64-
radioValue2: 'radioValue2',
65-
radioChecked1: true,
66-
radioChecked2: false
48+
list: [{
49+
array: [0]
50+
}, {
51+
array: [0, 1]
52+
}, {
53+
array: [0, 1, 2]
54+
}]
6755
}
6856
bs = BS.bind(data, tpl, function(content){
6957
$('#demo').append(content)
7058
})
7159
task = function(){
72-
data.radioChecked1 = false
73-
data.radioChecked2 = true
60+
data.list[1].array.sort(function(a, b) {
61+
return b - a
62+
})
7463
}
7564
setTimeout(function(){
7665
// data.radioChecked1 = false

dist/bisheng.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ define(
6363
if (data !== undefined && task.data !== data) continue
6464
if (tpl !== undefined && task.tpl !== tpl) continue
6565

66-
if (DEBUG) console.group('task ' + i)
67-
task()
68-
if (DEBUG) console.groupEnd('task ' + i)
66+
task(i)
6967
}
7068
if (AUTO) timerId = setTimeout(letMeSee, DURATION)
7169
}
@@ -157,20 +155,22 @@ define(
157155
var id = guid++;
158156
var shadow = clone(data, autoboxing, [id])
159157

160-
function task() {
158+
function task(index) {
159+
if (DEBUG) console.group('task ' + index)
161160
if (DEBUG) console.time(DEBUG.fix('diff'))
162161
var result = diff(data, shadow, autoboxing ? [id] : [], autoboxing)
163162
if (DEBUG) console.timeEnd(DEBUG.fix('diff'))
164163

165164
if (result && result.length) {
166165
fn(result, data, shadow)
167166

168-
if (DEBUG) console.time(DEBUG.fix('shadow'))
167+
if (DEBUG) console.time(DEBUG.fix('clone shadow'))
169168
shadow = clone(data, autoboxing, [id])
170-
if (DEBUG) console.timeEnd(DEBUG.fix('shadow'))
169+
if (DEBUG) console.timeEnd(DEBUG.fix('clone shadow'))
171170
}
171+
if (DEBUG) console.groupEnd('task ' + index)
172172
}
173-
task = _.throttle(task, 10)
173+
task = _.throttle(task, DURATION)
174174
task.data = data
175175
task.callback = fn
176176
if (fn && fn.tpl) task.tpl = fn.tpl
@@ -1178,6 +1178,14 @@ define(
11781178

11791179
// TODO 为什么不触发 change 事件?
11801180
$(target).on('change.bisheng keyup.bisheng', function(event) {
1181+
// 忽略不产生输入的辅助按键
1182+
if (event.type === 'keyup') {
1183+
var key = event.keyCode
1184+
1185+
// command modifiers arrows
1186+
if (key === 91 || (15 < key && key < 19) || (37 <= key && key <= 40)) return
1187+
}
1188+
11811189
updateValue(data, path, event.target)
11821190
if (!Loop.auto()) Loop.letMeSee(data, tpl)
11831191
})
@@ -1453,6 +1461,7 @@ define(
14531461
if ((change.type === 'delete' || change.type === 'add') && change.context instanceof Array) { /*paths.length === 0 && */
14541462
change.path.pop()
14551463
change.type = 'update'
1464+
change.value = change.context
14561465
change.context = change.getContext(change.root, change.path)()
14571466
handle(event, change, defined, context, options)
14581467
return
@@ -1499,9 +1508,9 @@ define(
14991508
var label
15001509
if (DEBUG) label = DEBUG.fix(guid, 4) + DEBUG.fix(type, 16) + DEBUG.fix(change.path.join('.'), 32)
15011510
if (DEBUG) console.group(label)
1502-
if (DEBUG) console.time(DEBUG.fix(''))
1511+
if (DEBUG) console.time(DEBUG.fix('Flush.handle[' + type + ']'))
15031512
if (handle[type]) handle[type](path, event, change, defined, options)
1504-
if (DEBUG) console.timeEnd(DEBUG.fix(''))
1513+
if (DEBUG) console.timeEnd(DEBUG.fix('Flush.handle[' + type + ']'))
15051514
if (DEBUG) console.groupEnd(label)
15061515
})
15071516
}
@@ -1944,7 +1953,7 @@ define(
19441953
}
19451954

19461955
var label
1947-
if (DEBUG) label = DEBUG.fix('flush changes[' + index + '] ' + change.path.join('.'))
1956+
if (DEBUG) label = DEBUG.fix('Flush.handle changes[' + index + '] ' + change.path.join('.'))
19481957
if (DEBUG) console.time(label)
19491958
if (DEBUG) console.group(label)
19501959
Flush.handle(event, change, clone, context, options)

src/brix/bisheng.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ define(
108108
}
109109

110110
var label
111-
if (DEBUG) label = DEBUG.fix('flush changes[' + index + '] ' + change.path.join('.'))
111+
if (DEBUG) label = DEBUG.fix('Flush.handle changes[' + index + '] ' + change.path.join('.'))
112112
if (DEBUG) console.time(label)
113113
if (DEBUG) console.group(label)
114114
Flush.handle(event, change, clone, context, options)

src/brix/bisheng/flush.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ define(
7575
if ((change.type === 'delete' || change.type === 'add') && change.context instanceof Array) { /*paths.length === 0 && */
7676
change.path.pop()
7777
change.type = 'update'
78+
change.value = change.context
7879
change.context = change.getContext(change.root, change.path)()
7980
handle(event, change, defined, context, options)
8081
return
@@ -121,9 +122,9 @@ define(
121122
var label
122123
if (DEBUG) label = DEBUG.fix(guid, 4) + DEBUG.fix(type, 16) + DEBUG.fix(change.path.join('.'), 32)
123124
if (DEBUG) console.group(label)
124-
if (DEBUG) console.time(DEBUG.fix(''))
125+
if (DEBUG) console.time(DEBUG.fix('Flush.handle[' + type + ']'))
125126
if (handle[type]) handle[type](path, event, change, defined, options)
126-
if (DEBUG) console.timeEnd(DEBUG.fix(''))
127+
if (DEBUG) console.timeEnd(DEBUG.fix('Flush.handle[' + type + ']'))
127128
if (DEBUG) console.groupEnd(label)
128129
})
129130
}

src/brix/bisheng/loop.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ define(
6262
if (data !== undefined && task.data !== data) continue
6363
if (tpl !== undefined && task.tpl !== tpl) continue
6464

65-
if (DEBUG) console.group('task ' + i)
66-
task()
67-
if (DEBUG) console.groupEnd('task ' + i)
65+
task(i)
6866
}
6967
if (AUTO) timerId = setTimeout(letMeSee, DURATION)
7068
}
@@ -156,20 +154,22 @@ define(
156154
var id = guid++;
157155
var shadow = clone(data, autoboxing, [id])
158156

159-
function task() {
157+
function task(index) {
158+
if (DEBUG) console.group('task ' + index)
160159
if (DEBUG) console.time(DEBUG.fix('diff'))
161160
var result = diff(data, shadow, autoboxing ? [id] : [], autoboxing)
162161
if (DEBUG) console.timeEnd(DEBUG.fix('diff'))
163162

164163
if (result && result.length) {
165164
fn(result, data, shadow)
166165

167-
if (DEBUG) console.time(DEBUG.fix('shadow'))
166+
if (DEBUG) console.time(DEBUG.fix('clone shadow'))
168167
shadow = clone(data, autoboxing, [id])
169-
if (DEBUG) console.timeEnd(DEBUG.fix('shadow'))
168+
if (DEBUG) console.timeEnd(DEBUG.fix('clone shadow'))
170169
}
170+
if (DEBUG) console.groupEnd('task ' + index)
171171
}
172-
task = _.throttle(task, 10)
172+
task = _.throttle(task, DURATION)
173173
task.data = data
174174
task.callback = fn
175175
if (fn && fn.tpl) task.tpl = fn.tpl

src/brix/bisheng/scan.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ define(
197197

198198
// TODO 为什么不触发 change 事件?
199199
$(target).on('change.bisheng keyup.bisheng', function(event) {
200+
// 忽略不产生输入的辅助按键
201+
if (event.type === 'keyup') {
202+
var key = event.keyCode
203+
204+
// command modifiers arrows
205+
if (key === 91 || (15 < key && key < 19) || (37 <= key && key <= 40)) return
206+
}
207+
200208
updateValue(data, path, event.target)
201209
if (!Loop.auto()) Loop.letMeSee(data, tpl)
202210
})

test/test.bisheng.expression.js

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,147 @@ describe('Expression', function() {
146146
}
147147
bindThenCheck(data, tpl, task, expected, done)
148148
})
149+
150+
it('array push', function(done) {
151+
var tpl = '{{#each list}}<p>{{array}}</p>{{/each}}'
152+
var data = {
153+
list: [{
154+
array: [0]
155+
}, {
156+
array: [0, 1]
157+
}, {
158+
array: [0, 1, 2]
159+
}]
160+
}
161+
var task = function() {
162+
data.list.push({
163+
array: [0, 1, 2, 3]
164+
})
165+
}
166+
var before = function(container) {
167+
expect(container.find('p')).to.have.length(3)
168+
}
169+
var expected = function(container) {
170+
expect(container.find('p')).to.have.length(4)
171+
}
172+
bindThenCheck(data, tpl, task, expected, done, before)
173+
})
174+
it('array push push', function(done) {
175+
var tpl = '{{#each list}}<p>{{array}}</p>{{/each}}'
176+
var data = {
177+
list: [{
178+
array: [0]
179+
}, {
180+
array: [0, 1]
181+
}, {
182+
array: [0, 1, 2]
183+
}]
184+
}
185+
var task = function() {
186+
data.list[0].array.push(1)
187+
}
188+
var before = function(container) {
189+
expect(container.find('p:eq(0)').text()).to.equal('0')
190+
}
191+
var expected = function(container) {
192+
expect(container.find('p:eq(0)').text()).to.equal('0,1')
193+
}
194+
bindThenCheck(data, tpl, task, expected, done, before)
195+
})
196+
it('array pop', function(done) {
197+
var tpl = '{{#each list}}<p>{{array}}</p>{{/each}}'
198+
var data = {
199+
list: [{
200+
array: [0]
201+
}, {
202+
array: [0, 1]
203+
}, {
204+
array: [0, 1, 2]
205+
}]
206+
}
207+
var task = function() {
208+
data.list.pop()
209+
}
210+
var before = function(container) {
211+
expect(container.find('p')).to.have.length(3)
212+
}
213+
var expected = function(container) {
214+
expect(container.find('p')).to.have.length(2)
215+
}
216+
bindThenCheck(data, tpl, task, expected, done, before)
217+
})
218+
it('array pop pop', function(done) {
219+
var tpl = '{{#each list}}<p>{{array}}</p>{{/each}}'
220+
var data = {
221+
list: [{
222+
array: [0]
223+
}, {
224+
array: [0, 1]
225+
}, {
226+
array: [0, 1, 2]
227+
}]
228+
}
229+
var task = function() {
230+
data.list[1].array.pop()
231+
}
232+
var before = function(container) {
233+
expect(container.find('p:eq(1)').text()).to.equal('0,1')
234+
}
235+
var expected = function(container) {
236+
expect(container.find('p:eq(1)').text()).to.equal('0')
237+
}
238+
bindThenCheck(data, tpl, task, expected, done, before)
239+
})
240+
it('array sort', function(done) {
241+
var tpl = '{{#each list}}<p>{{array}}</p>{{/each}}'
242+
var data = {
243+
list: [{
244+
array: [0]
245+
}, {
246+
array: [0, 1]
247+
}, {
248+
array: [0, 1, 2]
249+
}]
250+
}
251+
var task = function() {
252+
data.list.sort(function(a, b) {
253+
return b.array.length - a.array.length
254+
})
255+
}
256+
var before = function(container) {
257+
expect(container.find('p:eq(0)').text()).to.equal('0')
258+
expect(container.find('p:eq(1)').text()).to.equal('0,1')
259+
expect(container.find('p:eq(2)').text()).to.equal('0,1,2')
260+
}
261+
var expected = function(container) {
262+
expect(container.find('p:eq(2)').text()).to.equal('0')
263+
expect(container.find('p:eq(1)').text()).to.equal('0,1')
264+
expect(container.find('p:eq(0)').text()).to.equal('0,1,2')
265+
}
266+
bindThenCheck(data, tpl, task, expected, done, before)
267+
})
268+
// it('array sort sort', function(done) {
269+
// var tpl = '{{#each list}}<p>{{array}}</p>{{/each}}'
270+
// var data = {
271+
// list: [{
272+
// array: [0]
273+
// }, {
274+
// array: [0, 1]
275+
// }, {
276+
// array: [0, 1, 2]
277+
// }]
278+
// }
279+
// var task = function() {
280+
// data.list[1].array.sort(function(a, b) {
281+
// return b - a
282+
// })
283+
// }
284+
// var before = function(container) {
285+
// expect(container.find('p:eq(1)').text()).to.equal('0,1')
286+
// }
287+
// var expected = function(container) {
288+
// expect(container.find('p:eq(1)').text()).to.equal('1,0')
289+
// }
290+
// bindThenCheck(data, tpl, task, expected, done, before)
291+
// })
149292
})

0 commit comments

Comments
 (0)