This repository has been archived on 2023-08-29. You can view files and clone it, but cannot push or open issues/pull-requests.
yutent
/
anot.js
Archived
1
0
Fork 0
anot.js/src/27-:on.js

47 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-08-04 16:26:50 +08:00
var rdash = /\(([^)]*)\)/
var onDir = Anot.directive('on', {
priority: 3000,
init: function(binding) {
var value = binding.expr
binding.type = 'on'
var eventType = binding.param.replace(/-\d+$/, '') // :on-mousemove-10
if (typeof onDir[eventType + 'Hook'] === 'function') {
onDir[eventType + 'Hook'](binding)
}
if (value.indexOf('(') > 0 && value.indexOf(')') > -1) {
var matched = (value.match(rdash) || ['', ''])[1].trim()
if (matched === '' || matched === '$event') {
// aaa() aaa($event)当成aaa处理
value = value.replace(rdash, '')
}
}
binding.expr = value
},
update: function(callback) {
var binding = this
var elem = this.element
callback = function(e) {
var fn = binding.getter || noop
return fn.apply(binding.args[0], binding.args.concat(e))
}
var eventType = binding.param.replace(/-\d+$/, '') // :on-mousemove-10
if (eventType === 'scan') {
callback.call(elem, {
type: eventType
})
} else if (typeof binding.specialBind === 'function') {
binding.specialBind(elem, callback)
} else {
var removeFn = Anot.bind(elem, eventType, callback)
}
binding.rollback = function() {
if (typeof binding.specialUnbind === 'function') {
binding.specialUnbind()
} else {
Anot.unbind(elem, eventType, removeFn)
}
}
}
})