增加Props类型限制;手动扫描DOM改为自动
parent
6c7c96cddd
commit
c06d29dae1
|
@ -172,33 +172,24 @@
|
||||||
: typeof obj
|
: typeof obj
|
||||||
}
|
}
|
||||||
|
|
||||||
Anot.PropsTypes = {
|
Anot.PropsTypes = function(type) {
|
||||||
isString: function(val) {
|
this.type = 'PropsTypes'
|
||||||
function aa() {
|
this.checkType = type
|
||||||
return Anot.type(val) === 'string'
|
|
||||||
}
|
}
|
||||||
aa.type = 'PropsTypes'
|
|
||||||
|
|
||||||
return aa()
|
Anot.PropsTypes.prototype = {
|
||||||
|
toString: function() {
|
||||||
|
return ''
|
||||||
},
|
},
|
||||||
isNumber: function(val) {
|
check: function(val) {
|
||||||
this.type = 'PropsTypes'
|
return Anot.type(val) === this.checkType
|
||||||
return Anot.type(val) === 'number'
|
|
||||||
},
|
|
||||||
isArray: function(val) {
|
|
||||||
this.type = 'PropsTypes'
|
|
||||||
return Anot.type(val) === 'array'
|
|
||||||
},
|
|
||||||
isObject: function(val) {
|
|
||||||
this.type = 'PropsTypes'
|
|
||||||
return Anot.type(val) === 'object'
|
|
||||||
},
|
|
||||||
isFunction: function(val) {
|
|
||||||
this.type = 'PropsTypes'
|
|
||||||
return Anot.type(val) === 'function'
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Anot.PropsTypes.isString = function() {
|
||||||
|
return new this('string')
|
||||||
|
}
|
||||||
|
|
||||||
/*判定是否是一个朴素的javascript对象(Object),不是DOM对象,不是BOM对象,不是自定义类的实例*/
|
/*判定是否是一个朴素的javascript对象(Object),不是DOM对象,不是BOM对象,不是自定义类的实例*/
|
||||||
Anot.isPlainObject = function(obj) {
|
Anot.isPlainObject = function(obj) {
|
||||||
// 简单的 typeof obj === "object"检测,会致使用isPlainObject(window)在opera下通不过
|
// 简单的 typeof obj === "object"检测,会致使用isPlainObject(window)在opera下通不过
|
||||||
|
@ -218,7 +209,23 @@
|
||||||
}
|
}
|
||||||
vm = modelFactory(Object.assign({ props: {} }, source))
|
vm = modelFactory(Object.assign({ props: {} }, source))
|
||||||
vm.$id = $id
|
vm.$id = $id
|
||||||
return (VMODELS[$id] = vm)
|
VMODELS[$id] = vm
|
||||||
|
|
||||||
|
var $elem = document.querySelector('[anot=' + vm.$id + ']')
|
||||||
|
if ($elem) {
|
||||||
|
if ($elem === DOC.body) {
|
||||||
|
scanTag($elem, [])
|
||||||
|
} else {
|
||||||
|
var $parent = null
|
||||||
|
while (($parent = $elem.parentNode)) {
|
||||||
|
if ($parent.anotctrl) {
|
||||||
|
scanTag($elem.parentNode, [VMODELS[$parent.anotctrl]])
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return vm
|
||||||
} else {
|
} else {
|
||||||
this[0] = this.element = source
|
this[0] = this.element = source
|
||||||
}
|
}
|
||||||
|
@ -1342,13 +1349,13 @@
|
||||||
var simple = []
|
var simple = []
|
||||||
var $skipArray = {}
|
var $skipArray = {}
|
||||||
// 提取 source中的配置项, 并删除相应字段
|
// 提取 source中的配置项, 并删除相应字段
|
||||||
var data = source.data
|
var state = source.state
|
||||||
var computed = source.computed
|
var computed = source.computed
|
||||||
var methods = source.methods
|
var methods = source.methods
|
||||||
var props = source.props
|
var props = source.props
|
||||||
var watches = source.watch
|
var watches = source.watch
|
||||||
|
|
||||||
delete source.data
|
delete source.state
|
||||||
delete source.computed
|
delete source.computed
|
||||||
delete source.methods
|
delete source.methods
|
||||||
delete source.props
|
delete source.props
|
||||||
|
@ -1360,14 +1367,14 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// 基础数据
|
// 基础数据
|
||||||
if (data) {
|
if (state) {
|
||||||
if (source.$id) {
|
if (source.$id) {
|
||||||
// 直接删除名为props的 字段, 对于主VM对象, props将作为保留关键字
|
// 直接删除名为props的 字段, 对于主VM对象, props将作为保留关键字
|
||||||
// 下面的计算属性,方法等, 作同样的逻辑处理
|
// 下面的计算属性,方法等, 作同样的逻辑处理
|
||||||
delete data.props
|
delete state.props
|
||||||
}
|
}
|
||||||
for (name in data) {
|
for (name in state) {
|
||||||
var value = data[name]
|
var value = state[name]
|
||||||
if (!$$skipArray[name]) {
|
if (!$$skipArray[name]) {
|
||||||
hasOwn[name] = true
|
hasOwn[name] = true
|
||||||
}
|
}
|
||||||
|
@ -1442,7 +1449,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.assign(source, data, methods)
|
Object.assign(source, state, methods)
|
||||||
|
|
||||||
accessors['$model'] = $modelDescriptor
|
accessors['$model'] = $modelDescriptor
|
||||||
$vmodel = Object.defineProperties($vmodel, accessors, source)
|
$vmodel = Object.defineProperties($vmodel, accessors, source)
|
||||||
|
@ -1456,7 +1463,7 @@
|
||||||
/* jshint ignore:start */
|
/* jshint ignore:start */
|
||||||
// hideProperty($vmodel, '$ups', null)
|
// hideProperty($vmodel, '$ups', null)
|
||||||
hideProperty($vmodel, '$id', 'anonymous')
|
hideProperty($vmodel, '$id', 'anonymous')
|
||||||
// hideProperty($vmodel, '$up', old ? old.$up : null)
|
hideProperty($vmodel, '$up', old ? old.$up : null)
|
||||||
hideProperty($vmodel, '$track', Object.keys(hasOwn))
|
hideProperty($vmodel, '$track', Object.keys(hasOwn))
|
||||||
hideProperty($vmodel, '$active', false)
|
hideProperty($vmodel, '$active', false)
|
||||||
hideProperty($vmodel, '$pathname', old ? old.$pathname : '')
|
hideProperty($vmodel, '$pathname', old ? old.$pathname : '')
|
||||||
|
@ -1483,7 +1490,7 @@
|
||||||
//必须设置了$active,$events
|
//必须设置了$active,$events
|
||||||
simple.forEach(function(name) {
|
simple.forEach(function(name) {
|
||||||
var oldVal = old && old[name]
|
var oldVal = old && old[name]
|
||||||
var val = ($vmodel[name] = data[name])
|
var val = ($vmodel[name] = state[name])
|
||||||
if (val && typeof val === 'object') {
|
if (val && typeof val === 'object') {
|
||||||
val.$up = $vmodel
|
val.$up = $vmodel
|
||||||
val.$pathname = name
|
val.$pathname = name
|
||||||
|
@ -1581,7 +1588,7 @@
|
||||||
old.$active = false
|
old.$active = false
|
||||||
}
|
}
|
||||||
return observeObject(
|
return observeObject(
|
||||||
{ data: obj },
|
{ state: obj },
|
||||||
{
|
{
|
||||||
old: old,
|
old: old,
|
||||||
watch: watch
|
watch: watch
|
||||||
|
@ -1608,7 +1615,7 @@
|
||||||
|
|
||||||
array._ = observeObject(
|
array._ = observeObject(
|
||||||
{
|
{
|
||||||
data: { length: NaN }
|
state: { length: NaN }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
watch: true
|
watch: true
|
||||||
|
@ -2950,12 +2957,6 @@
|
||||||
* 扫描系统 *
|
* 扫描系统 *
|
||||||
**********************************************************************/
|
**********************************************************************/
|
||||||
|
|
||||||
Anot.scan = function(elem, vmodel) {
|
|
||||||
elem = elem || root
|
|
||||||
var vmodels = vmodel ? [].concat(vmodel) : []
|
|
||||||
scanTag(elem, vmodels)
|
|
||||||
}
|
|
||||||
|
|
||||||
//http://www.w3.org/TR/html5/syntax.html#void-elements
|
//http://www.w3.org/TR/html5/syntax.html#void-elements
|
||||||
var stopScan = oneObject(
|
var stopScan = oneObject(
|
||||||
'area,base,basefont,br,col,command,embed,hr,img,input,link,meta,param,source,track,wbr,noscript,script,style,textarea'.toUpperCase()
|
'area,base,basefont,br,col,command,embed,hr,img,input,link,meta,param,source,track,wbr,noscript,script,style,textarea'.toUpperCase()
|
||||||
|
@ -2974,11 +2975,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function createSignalTower(elem, vmodel) {
|
function createSignalTower(elem, vmodel) {
|
||||||
var id = elem.getAttribute('anotctrl') || vmodel.$id
|
elem.anotctrl = elem.anotctrl || vmodel.$id
|
||||||
elem.setAttribute('anotctrl', id)
|
|
||||||
if (vmodel.$events) {
|
|
||||||
vmodel.$events.expr = elem.tagName + '[anotctrl="' + id + '"]'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getBindingCallback(elem, name, vmodels) {
|
function getBindingCallback(elem, name, vmodels) {
|
||||||
|
@ -3216,7 +3213,6 @@
|
||||||
//--> :if-loop(110) --> :attr(970) ...--> :each(1400)-->:with(1500)--〉:duplex(2000)垫后
|
//--> :if-loop(110) --> :attr(970) ...--> :each(1400)-->:with(1500)--〉:duplex(2000)垫后
|
||||||
var skip = elem.getAttribute('skip')
|
var skip = elem.getAttribute('skip')
|
||||||
node = elem.getAttributeNode('anot')
|
node = elem.getAttributeNode('anot')
|
||||||
|
|
||||||
var vm = vmodels.concat()
|
var vm = vmodels.concat()
|
||||||
if (typeof skip === 'string') {
|
if (typeof skip === 'string') {
|
||||||
return
|
return
|
||||||
|
@ -3233,21 +3229,44 @@
|
||||||
elem.removeAttribute(node.name) //removeAttributeNode不会刷新xx[anot]样式规则
|
elem.removeAttribute(node.name) //removeAttributeNode不会刷新xx[anot]样式规则
|
||||||
createSignalTower(elem, newVmodel)
|
createSignalTower(elem, newVmodel)
|
||||||
hideProperty(newVmodel, '$elem', elem)
|
hideProperty(newVmodel, '$elem', elem)
|
||||||
log(attrs)
|
if (vmodels.length) {
|
||||||
attrs.forEach(function(attr, i) {
|
attrs.forEach(function(attr, i) {
|
||||||
log(attr, /^:/.test(attr.name), newVmodel.props)
|
|
||||||
if (/^:/.test(attr.name)) {
|
if (/^:/.test(attr.name)) {
|
||||||
var name = attr.name.replace(':', '')
|
var name = attr.name.replace(':', '')
|
||||||
var value = parseExpr(attr.value, vmodels, {}).apply(0, vmodels)
|
var value = null
|
||||||
|
if (Anot.directives[name]) {
|
||||||
if (newVmodel.props[name].call(Anot.PropsTypes, value)) {
|
return
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
value = parseExpr(attr.value, vmodels, {}).apply(0, vmodels)
|
||||||
|
elem.removeAttribute(attr.name)
|
||||||
|
} catch (error) {
|
||||||
|
log(
|
||||||
|
'Props parse faild on (%s[class=%s]),',
|
||||||
|
elem.nodeName,
|
||||||
|
elem.className,
|
||||||
|
attr,
|
||||||
|
error + ''
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (!value) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
newVmodel.props[name] &&
|
||||||
|
newVmodel.props[name].type === 'PropsTypes'
|
||||||
|
) {
|
||||||
|
if (newVmodel.props[name].check(value)) {
|
||||||
newVmodel.props[name] = value
|
newVmodel.props[name] = value
|
||||||
} else {
|
} else {
|
||||||
Anot.error('props「' + name + '」类型错误!' + value)
|
Anot.error('props「' + name + '」类型错误!' + value, TypeError)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
newVmodel.props[name] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// newVmodel.props.aaa = 8999
|
}
|
||||||
}
|
}
|
||||||
scanAttr(elem, vm) //扫描特性节点
|
scanAttr(elem, vm) //扫描特性节点
|
||||||
|
|
||||||
|
@ -3702,7 +3721,7 @@
|
||||||
})
|
})
|
||||||
|
|
||||||
//这几个指令都可以使用插值表达式,如:src="aaa/{{b}}/{{c}}.html"
|
//这几个指令都可以使用插值表达式,如:src="aaa/{{b}}/{{c}}.html"
|
||||||
'title,alt,src,value,css,include,href,data'.replace(rword, function(name) {
|
'css,include,data'.replace(rword, function(name) {
|
||||||
directives[name] = attrDir
|
directives[name] = attrDir
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -5424,7 +5443,7 @@
|
||||||
}
|
}
|
||||||
force[itemName] = 1
|
force[itemName] = 1
|
||||||
var proxy = modelFactory(
|
var proxy = modelFactory(
|
||||||
{ data: source },
|
{ state: source },
|
||||||
{
|
{
|
||||||
force: force
|
force: force
|
||||||
}
|
}
|
||||||
|
@ -5456,9 +5475,12 @@
|
||||||
$index: 1
|
$index: 1
|
||||||
}
|
}
|
||||||
force[itemName] = 1
|
force[itemName] = 1
|
||||||
var proxy = modelFactory(source, {
|
var proxy = modelFactory(
|
||||||
|
{ state: source },
|
||||||
|
{
|
||||||
force: force
|
force: force
|
||||||
})
|
}
|
||||||
|
)
|
||||||
proxy.$id = generateID('$proxy$with')
|
proxy.$id = generateID('$proxy$with')
|
||||||
return proxy
|
return proxy
|
||||||
}
|
}
|
||||||
|
@ -6438,7 +6460,7 @@
|
||||||
loader: true
|
loader: true
|
||||||
})
|
})
|
||||||
Anot.ready(function() {
|
Anot.ready(function() {
|
||||||
Anot.scan(DOC.body)
|
scanTag(DOC.body, [])
|
||||||
})
|
})
|
||||||
|
|
||||||
if (typeof define === 'function' && define.amd) {
|
if (typeof define === 'function' && define.amd) {
|
||||||
|
|
Reference in New Issue