Skip to content

Commit 0982349

Browse files
committed
alias replace -> set on observed Arrays
1 parent e16f910 commit 0982349

File tree

1 file changed

+47
-40
lines changed

1 file changed

+47
-40
lines changed

src/observer.js

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,54 +41,61 @@ methods.forEach(function (method) {
4141
}, !hasProto)
4242
})
4343

44-
// Augment it with several convenience methods
45-
var extensions = {
46-
remove: function (index) {
47-
if (typeof index === 'function') {
48-
var i = this.length,
49-
removed = []
50-
while (i--) {
51-
if (index(this[i])) {
52-
removed.push(this.splice(i, 1)[0])
53-
}
54-
}
55-
return removed.reverse()
56-
} else {
57-
if (typeof index !== 'number') {
58-
index = this.indexOf(index)
59-
}
60-
if (index > -1) {
61-
return this.splice(index, 1)[0]
44+
/**
45+
* Convenience method to remove an element in an Array
46+
* This will be attached to observed Array instances
47+
*/
48+
function removeElement (index) {
49+
if (typeof index === 'function') {
50+
var i = this.length,
51+
removed = []
52+
while (i--) {
53+
if (index(this[i])) {
54+
removed.push(this.splice(i, 1)[0])
6255
}
6356
}
64-
},
65-
replace: function (index, data) {
66-
if (typeof index === 'function') {
67-
var i = this.length,
68-
replaced = [],
69-
replacer
70-
while (i--) {
71-
replacer = index(this[i])
72-
if (replacer !== undefined) {
73-
replaced.push(this.splice(i, 1, replacer)[0])
74-
}
75-
}
76-
return replaced.reverse()
77-
} else {
78-
if (typeof index !== 'number') {
79-
index = this.indexOf(index)
80-
}
81-
if (index > -1) {
82-
return this.splice(index, 1, data)[0]
83-
}
57+
return removed.reverse()
58+
} else {
59+
if (typeof index !== 'number') {
60+
index = this.indexOf(index)
61+
}
62+
if (index > -1) {
63+
return this.splice(index, 1)[0]
8464
}
8565
}
8666
}
8767

88-
for (var method in extensions) {
89-
def(ArrayProxy, method, extensions[method], !hasProto)
68+
/**
69+
* Convenience method to replace an element in an Array
70+
* This will be attached to observed Array instances
71+
*/
72+
function replaceElement (index, data) {
73+
if (typeof index === 'function') {
74+
var i = this.length,
75+
replaced = [],
76+
replacer
77+
while (i--) {
78+
replacer = index(this[i])
79+
if (replacer !== undefined) {
80+
replaced.push(this.splice(i, 1, replacer)[0])
81+
}
82+
}
83+
return replaced.reverse()
84+
} else {
85+
if (typeof index !== 'number') {
86+
index = this.indexOf(index)
87+
}
88+
if (index > -1) {
89+
return this.splice(index, 1, data)[0]
90+
}
91+
}
9092
}
9193

94+
// Augment the ArrayProxy with convenience methods
95+
def(ArrayProxy, 'remove', removeElement, !hasProto)
96+
def(ArrayProxy, 'set', replaceElement, !hasProto)
97+
def(ArrayProxy, 'replace', replaceElement, !hasProto)
98+
9299
/**
93100
* Watch an Object, recursive.
94101
*/

0 commit comments

Comments
 (0)