Skip to content

Commit

Permalink
让所有方法都共用一套方法
Browse files Browse the repository at this point in the history
  • Loading branch information
qincheng committed Sep 20, 2013
1 parent 39ce801 commit 2a39d5f
Show file tree
Hide file tree
Showing 5 changed files with 613 additions and 780 deletions.
185 changes: 93 additions & 92 deletions avalon.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@
}
if (!rnative.test([].map)) {
avalon.mix(Array.prototype, {
//定位操作,返回数组中第一个等于给定参数的元素的索引值。
//定位操作,返回数组中第一个等于给定参数的元素的索引值。
indexOf: function(item, index) {
var n = this.length,
i = ~~index
Expand Down Expand Up @@ -2489,74 +2489,59 @@
return -1
}

function Collection(model) {
var array = []
array.$id = generateID()
array[subscribers] = []
array.$model = model
array.$events = {} //VB对象的方法里的this并不指向自身,需要使用bind处理一下
array._splice = array.splice
for (var i in Observable) {
array[i] = Observable[i]
}
var dynamic = modelFactory({
length: model.length
})
dynamic.$watch("length", function(a, b) {
array.$fire("length", a, b)
})

array._add = function(arr, pos) {
pos = typeof pos === "number" ? pos : this.length;
var _splice = [].splice
var CollectionPrototype = {
_splice: _splice,
_add: function(arr, pos) {
var oldLength = this.length
pos = typeof pos === "number" ? pos : oldLength;
var added = []
for (var i = 0, n = arr.length; i < n; i++) {
added[i] = convert(arr[i])
}
this._splice.apply(this, [pos, 0].concat(added))
_splice.apply(this, [pos, 0].concat(added))
notifySubscribers(this, "add", added, pos)
if (!this.stopFireLength) {
return dynamic.length = this.length
if (!this._stopFireLength) {
return this._.length = this.length
}
}

array._del = function(pos, n) {
},
_del: function(pos, n) {
var ret = this._splice(pos, n)
if (ret.length) {
notifySubscribers(this, "del", pos, n)
if (!this.stopFireLength) {
dynamic.length = this.length
if (!this._stopFireLength) {
this._.length = this.length
}
}
return ret
}
array.push = function() {
model.push.apply(model, arguments)
},
push: function() {
[].push.apply(this.$model, arguments)
return this._add(arguments) //返回长度
}
array.unshift = function() {
model.unshift.apply(model, arguments)
},
unshift: function() {
[].unshift.apply(this.$model, arguments)
var ret = this._add(arguments, 0) //返回长度
notifySubscribers(this, "index", arguments.length)
return ret
}
array.shift = function() {
model.shift()
var el = this._del(0, 1)
},
shift: function() {
var el = this.$model.shift()
this._del(0, 1)
notifySubscribers(this, "index", 0)
return el[0] //返回被移除的元素
}
array.pop = function() {
var el = model.pop()
return el//返回被移除的元素
},
pop: function() {
var el = this.$model.pop()
this._del(this.length - 1, 1)
return el[0] //返回被移除的元素
}

array.splice = function(a, b) {
return el //返回被移除的元素
},
splice: function(a, b) {
// 必须存在第一个参数,需要大于-1, 为添加或删除元素的基点
a = resetNumber(a, this.length)
var removed = model.splice.apply(model, arguments),
var removed = _splice.apply(this.$model, arguments),
ret = []
this.stopFireLength = true //确保在这个方法中 , $watch("length",fn)只触发一次
this._stopFireLength = true //确保在这个方法中 , $watch("length",fn)只触发一次
if (removed.length) {
ret = this._del(a, removed.length)
if (arguments.length <= 2) { //如果没有执行添加操作,需要手动resetIndex
Expand All @@ -2566,57 +2551,35 @@
if (arguments.length > 2) {
this._add(aslice.call(arguments, 2), a)
}
this.stopFireLength = false
dynamic.length = this.length
this._stopFireLength = false
this._.length = this.length
return ret //返回被移除的元素
}
"sort,reverse".replace(rword, function(method) {
array[method] = function() {
model[method].apply(model, arguments)
var sorted = false
for (var i = 0, n = this.length; i < n; i++) {
var a = model[i]
var b = this[i]
var b = b && b.$model ? b.$model : b
if (!isEqual(a, b)) {
sorted = true
var index = getVMIndex(a, this, i)
var remove = this._splice(index, 1)[0]
array._splice(i, 0, remove)
notifySubscribers(this, "move", index, i)
}
}
if (sorted) {
notifySubscribers(this, "index", 0)
}
return this
}
})
array.contains = function(el) { //判定是否包含
},
contains: function(el) { //判定是否包含
return this.indexOf(el) !== -1
}
array.size = function() { //取得数组长度,这个函数可以同步视图,length不能
return dynamic.length
}
array.remove = function(el) { //移除第一个等于给定值的元素
},
size: function() { //取得数组长度,这个函数可以同步视图,length不能
return this._.length
},
remove: function(el) { //移除第一个等于给定值的元素
var index = this.indexOf(el)
if (index >= 0) {
return this.removeAt(index)
}
}
array.removeAt = function(index) { //移除指定索引上的元素
},
removeAt: function(index) { //移除指定索引上的元素
this.splice(index, 1)
}
array.clear = function() {
this.$model.length = this.length = dynamic.length = 0 //清空数组
},
clear: function() {
this.$model.length = this.length = this._.length = 0 //清空数组
notifySubscribers(this, "clear")
return this
}
array.removeAll = function(all) { //移除N个元素
},
removeAll: function(all) { //移除N个元素
if (Array.isArray(all)) {
all.forEach(function(el) {
array.remove(el)
})
this.remove(el)
}, this)
} else if (typeof all === "function") {
for (var i = this.length - 1; i >= 0; i--) {
var el = this[i]
Expand All @@ -2627,14 +2590,14 @@
} else {
this.clear()
}
}
array.ensure = function(el) {
},
ensure: function(el) {
if (!this.contains(el)) { //只有不存在才push
this.push(el)
}
return this
}
array.set = function(index, val) {
},
set: function(index, val) {
if (index >= 0 && index < this.length) {
var valueType = getType(val)
if (rchecktype.test(valueType)) {
Expand All @@ -2649,6 +2612,44 @@
}
return this
}
}
"sort,reverse".replace(rword, function(method) {
CollectionPrototype[method] = function() {
[][method].apply(this.$model, arguments)
var sorted = false
for (var i = 0, n = this.length; i < n; i++) {
var a = this.$model[i], b = this[i]
b = b && b.$model ? b.$model : b
if (!isEqual(a, b)) {
sorted = true
var index = getVMIndex(a, this, i)
var remove = this._splice(index, 1)[0]
this._splice(i, 0, remove)
notifySubscribers(this, "move", index, i)
}
}
if (sorted) {
notifySubscribers(this, "index", 0)
}
return this
}
})
function Collection(model) {
var array = []
array.$id = generateID()
array[subscribers] = []
array.$model = model
array.$events = {} //VB对象的方法里的this并不指向自身,需要使用bind处理一下
array._ = modelFactory({
length: model.length
})
array._.$watch("length", function(a, b) {
array.$fire("length", a, b)
})
for (var i in Observable) {
array[i] = Observable[i]
}
avalon.mix(array, CollectionPrototype)
return array
}

Expand Down
Loading

0 comments on commit 2a39d5f

Please sign in to comment.