Skip to content

Commit 19d15ec

Browse files
author
Evan You
committed
Improvements to observed array extension methods
- mutateFilter() is now remove(fn) - replace() now also takes a function argument - tests for above - fix unit tests for IE9
1 parent 93b2cf9 commit 19d15ec

File tree

5 files changed

+106
-27
lines changed

5 files changed

+106
-27
lines changed

examples/todomvc/js/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ var app = new Seed({
7272
},
7373

7474
removeCompleted: function () {
75-
this.todos.mutateFilter(function (todo) {
76-
return !todo.completed
75+
this.todos.remove(function (todo) {
76+
return todo.completed
7777
})
7878
todoStorage.save()
7979
},

src/compiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ CompilerProto.compile = function (node, root) {
221221
var repeatExp,
222222
componentId,
223223
partialId,
224-
customElementFn = utils.elements[node.tagName.toLowerCase()]
224+
customElementFn = compiler.getOption('elements', node.tagName.toLowerCase())
225225

226226
// It is important that we access these attributes
227227
// procedurally because the order matters.

src/observer.js

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,44 @@ methods.forEach(function (method) {
3838
// Augment it with several convenience methods
3939
var extensions = {
4040
remove: function (index) {
41-
if (typeof index !== 'number') index = this.indexOf(index)
42-
return this.splice(index, 1)[0]
41+
if (typeof index === 'function') {
42+
var i = this.length,
43+
removed = []
44+
while (i--) {
45+
if (index(this[i])) {
46+
removed.push(this.splice(i, 1)[0])
47+
}
48+
}
49+
return removed.reverse()
50+
} else {
51+
if (typeof index !== 'number') {
52+
index = this.indexOf(index)
53+
}
54+
if (index > -1) {
55+
return this.splice(index, 1)[0]
56+
}
57+
}
4358
},
4459
replace: function (index, data) {
45-
if (typeof index !== 'number') index = this.indexOf(index)
46-
if (this[index] !== undefined) return this.splice(index, 1, data)[0]
47-
},
48-
mutateFilter: function (fn) {
49-
var i = this.length
50-
while (i--) {
51-
if (!fn(this[i])) this.splice(i, 1)
60+
if (typeof index === 'function') {
61+
var i = this.length,
62+
replaced = [],
63+
replacer
64+
while (i--) {
65+
replacer = index(this[i])
66+
if (replacer !== undefined) {
67+
replaced.push(this.splice(i, 1, replacer)[0])
68+
}
69+
}
70+
return replaced.reverse()
71+
} else {
72+
if (typeof index !== 'number') {
73+
index = this.indexOf(index)
74+
}
75+
if (index > -1) {
76+
return this.splice(index, 1, data)[0]
77+
}
5278
}
53-
return this
5479
}
5580
}
5681

test/unit/specs/directives.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,12 @@ describe('UNIT: Directives', function () {
389389
triggered = true
390390
}}
391391
dir.el.value = 'foo'
392+
document.body.appendChild(dir.el)
392393
dir.el.dispatchEvent(mockHTMLEvent('input'))
393394
// timeout becuase the update is async
394395
setTimeout(function () {
395396
assert.ok(triggered)
397+
document.body.removeChild(dir.el)
396398
done()
397399
}, 1)
398400
})

test/unit/specs/observer.js

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -263,44 +263,96 @@ describe('UNIT: Observer', function () {
263263
})
264264

265265
describe('Augmentations', function () {
266+
267+
it('remove (index)', function () {
268+
var emitted = false,
269+
index = ~~(Math.random() * arr.length),
270+
expected = arr[index] = { a: 1 }
271+
ob.once('mutate', function (key, array, mutation) {
272+
emitted = true
273+
assert.strictEqual(mutation.method, 'splice')
274+
assert.strictEqual(mutation.args.length, 2)
275+
assert.strictEqual(mutation.args[0], index)
276+
})
277+
var r = arr.remove(index)
278+
assert.ok(emitted)
279+
assert.strictEqual(r, expected)
280+
})
266281

267-
it('remove', function () {
282+
it('remove (object)', function () {
268283
var emitted = false,
269-
expected = arr[0] = { a: 1 }
284+
index = ~~(Math.random() * arr.length),
285+
expected = arr[index] = { a: 1 }
270286
ob.once('mutate', function (key, array, mutation) {
271287
emitted = true
272288
assert.strictEqual(mutation.method, 'splice')
273289
assert.strictEqual(mutation.args.length, 2)
290+
assert.strictEqual(mutation.args[0], index)
274291
})
275292
var r = arr.remove(expected)
276293
assert.ok(emitted)
277294
assert.strictEqual(r, expected)
278295
})
279296

280-
it('replace', function () {
297+
it('remove (function)', function () {
298+
var expected = [1001, 1002]
299+
arr.push.apply(arr, expected)
300+
var filter = function (e) {
301+
return e > 1000
302+
},
303+
copy = arr.filter(function (e) {
304+
return e <= 1000
305+
})
306+
var removed = arr.remove(filter)
307+
assert.deepEqual(arr, copy)
308+
assert.deepEqual(expected, removed)
309+
})
310+
311+
it('replace (index)', function () {
312+
var emitted = false,
313+
index = ~~(Math.random() * arr.length),
314+
expected = arr[index] = { a: 1 },
315+
arg = 34567
316+
ob.once('mutate', function (key, array, mutation) {
317+
emitted = true
318+
assert.strictEqual(mutation.method, 'splice')
319+
assert.strictEqual(mutation.args.length, 3)
320+
assert.strictEqual(mutation.args[0], index)
321+
})
322+
var r = arr.replace(index, arg)
323+
assert.ok(emitted)
324+
assert.strictEqual(r, expected)
325+
assert.strictEqual(arr[index], arg)
326+
})
327+
328+
it('replace (object)', function () {
281329
var emitted = false,
282-
expected = arr[0] = { a: 1 },
330+
index = ~~(Math.random() * arr.length),
331+
expected = arr[index] = { a: 1 },
283332
arg = 45678
284333
ob.once('mutate', function (key, array, mutation) {
285334
emitted = true
286335
assert.strictEqual(mutation.method, 'splice')
287336
assert.strictEqual(mutation.args.length, 3)
337+
assert.strictEqual(mutation.args[0], index)
288338
})
289339
var r = arr.replace(expected, arg)
290340
assert.ok(emitted)
291341
assert.strictEqual(r, expected)
292-
assert.strictEqual(arr[0], arg)
342+
assert.strictEqual(arr[index], arg)
293343
})
294344

295-
it('mutateFilter', function () {
296-
var filter = function (e) {
297-
return e > 1000
298-
},
299-
copy = arr.slice().filter(filter)
300-
arr.mutateFilter(filter)
301-
for (var i = 0; i < copy.length; i++) {
302-
assert.strictEqual(arr[i], copy[i])
303-
}
345+
it('replace (function)', function () {
346+
arr[0] = 1
347+
arr[1] = 2
348+
arr[2] = 3
349+
var expected = [2, 3, 3],
350+
expectRet = [1, 2]
351+
var replaced = arr.replace(function (e) {
352+
if (e < 3) return e + 1
353+
})
354+
assert.deepEqual(arr, expected)
355+
assert.deepEqual(replaced, expectRet)
304356
})
305357

306358
})

0 commit comments

Comments
 (0)