Skip to content

Commit

Permalink
update~ rregexp
Browse files Browse the repository at this point in the history
  • Loading branch information
RubyLouvre committed Jul 23, 2013
1 parent 9c4c064 commit 8825907
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 20 deletions.
4 changes: 2 additions & 2 deletions avalon.mobile.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,12 @@
}
return this
}
var openTag, closeTag, rexpr, rbind, rregexp = /([-.*+?^${}()|[\]\/\\])/g
var openTag, closeTag, rexpr, rbind, rregexp = rregexp = /[-.*+?^${}()|[\]\/\\]/g

function escapeRegExp(target) {
//http://stevenlevithan.com/regex/xregexp/
//将字符串安全格式化为正则表达式的源码
return (target + "").replace(rregexp, "\\$1");
return (target + "").replace(rregexp, "\\$&")
}
var plugins = {
alias: function(val) {
Expand Down
92 changes: 75 additions & 17 deletions avalon091.js
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@
}
}

function updateViewModel(a, b, isArray) {
function updateViewModel(a, b, isArray, accessor) {
if (isArray) {
var an = a.length,
bn = b.length
Expand All @@ -1384,25 +1384,70 @@
a.set(i, b[i])
}
} else {
// console.log()
var addedProperty = {}, modifyProperty = {}, flagAdd = false;
for (var i in b) {
if (b.hasOwnProperty(i) && a.hasOwnProperty(i) && i !== "$id") {
a[i] = b[i]
if (b.hasOwnProperty(i)) {
if (a.hasOwnProperty(i)) {
if (b[i] !== a[i]) {
modifyProperty[i] = b[i]
}
} else {
flagAdd = true;
addedProperty[i] = b[i]
}
}
}
if (flagAdd) {
var VBPublics = getVBPublics(unwatchOne, a.$skipArray || b.$skipArray)
var arr = descriptorFactory(modifyProperty, a.$model, VBPublics, {})
var callGetters = arr[0]
var callSetters = arr[1]
var descriptors = arr[2]
var vmodel = {}
vmodel = defineProperties(vmodel, descriptors, Object.keys(VBPublics))
for (var name in VBPublics) {
if (!unwatchOne[name]) {
vmodel[name] = modifyProperty[name]
}
}
for (var i in descriptors) {
descriptors[i].get.vmodel = vmodel
}
callSetters.forEach(function(prop) {
vmodel[prop] = modifyProperty[prop] //为空对象赋值
})
callGetters.forEach(function(fn) {
Publish[expose] = fn
callSetters = vmodel[fn.nick]
fn.locked = 1
delete Publish[expose]
})
vmodel.$model = vmodel.$json = a.$model
vmodel.$events = {} //VB对象的方法里的this并不指向自身,需要使用bind处理一下
vmodel.$id = generateID()
vmodel.$descriptors = descriptors
for (var i in Observable) {
vmodel[i] = Observable[i].bind(vmodel)
}
vmodel.hasOwnProperty = function(name) {
return name in vmodel.$model
}
return vmodel
}

}
}

var unwatchOne = oneObject("$id,$skipArray,$watch,$unwatch,$fire,$events,$json,$model,$descriptors")

function descriptorFactory(scope, model, vmodel, VBPublics, watchMore) {
function descriptorFactory(scope, model, VBPublics, watchMore) {
var callGetters = [], callSetters = [], descriptors = {}
function loop(name, value) {
if (!unwatchOne[name]) {
model[name] = value
}
var valueType = getType(value), descriptor, oldArgs
if (valueType === "function"|| (name.charAt(0) === "$" && !watchMore[name])) {
if (valueType === "function" || (name.charAt(0) === "$" && !watchMore[name])) {
VBPublics[name] = 1
}
if (VBPublics[name] === 1) {
Expand All @@ -1411,7 +1456,9 @@
if (valueType === "object" && typeof value.get === "function" && Object.keys(value).length <= 2) {
var setter = value.set,
getter = value.get

descriptor = function(neo) { //创建计算属性
var vmodel = descriptor.vmodel
if (arguments.length) {
if (stopRepeatAssign) {
return //阻止重复赋值
Expand Down Expand Up @@ -1443,7 +1490,8 @@
} else {
value = NaN
callSetters.push(name)
accessor = function(neo) { //创建监控属性或数组
descriptor = function(neo) { //创建监控属性或数组
var vmodel = descriptor.vmodel
if (arguments.length) {
if (stopRepeatAssign) {
return //阻止重复赋值
Expand Down Expand Up @@ -1472,7 +1520,11 @@
}
}
}
descriptor[subscribers] = []
try {
descriptor[subscribers] = []
} catch (e) {
console.log(name)
}
descriptors[name] = {
set: descriptor,
get: descriptor,
Expand All @@ -1482,24 +1534,27 @@
for (var i in scope) {
loop(i, scope[i])
}
return [callGetters, callSetters, accesses]
return [callGetters, callSetters, descriptors]
}
function getVBPublics(unwatchOne, $skipArray) {
var ret = avalon.mix({}, unwatchOne)
for (var name in $skipArray) {
if ($skipArray.hasOwnProperty(name)) {
ret[name] = 1
}
}
return ret;
}

function modelFactory(scope, model, watchMore) {
if (Array.isArray(scope)) {
return Collection(scope)
}
//一开始VBPublics里面全是忽略监控的属性
var VBPublics = avalon.mix({}, unwatchOne)
for (var name in scope.$skipArray) {
if (scope.$skipArray.hasOwnProperty(name)) {
VBPublics[name] = 1
}
}
var VBPublics = getVBPublics(unwatchOne, scope.$skipArray)
var vmodel = {}
model = model || {}
//在这里我们继续为VBPublics添加函数
var arr = descriptorFactory(scope, model, vmodel, VBPublics, watchMore || {})
var arr = descriptorFactory(scope, model, VBPublics, watchMore || {})
var callGetters = arr[0]
var callSetters = arr[1]
var descriptors = arr[2]
Expand All @@ -1509,6 +1564,9 @@
vmodel[name] = scope[name]
}
}
for (var i in descriptors) {
descriptors[i].get.vmodel = vmodel
}
callSetters.forEach(function(prop) {
vmodel[prop] = scope[prop] //为空对象赋值
})
Expand Down
2 changes: 1 addition & 1 deletion index2.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<title>scope</title>
<script src="avalon_1.js" type="text/javascript"></script>
<script src="avalon.js" type="text/javascript"></script>
<script>

avalon.ready(function() {
Expand Down

0 comments on commit 8825907

Please sign in to comment.