nuijs
Version:
nui框架
65 lines (61 loc) • 2.25 kB
JavaScript
Nui.define(['./extends'], function(){
return function(opts){
var self = this, that = opts || self,
constr = that.constructor,
isComponent = constr && constr.__component_name,
elem = self.element || that.element || Nui.doc,
events = isComponent ? that._events : that.events;
if(!elem || !events){
return that
}
if(typeof events === 'function'){
events = events.call(that)
}
if(!(elem instanceof jQuery)){
elem = jQuery(elem)
}
var evt, ele, ret;
var callback = function(e, elem, cbs){
if(typeof cbs === 'function'){
cbs.call(that, e, elem);
}
else{
var _cb, _that;
Nui.each(cbs, function(cb, i){
if(typeof (_cb = that[cb]) === 'function'){
_that = that;
}
else if(typeof (_cb = self[cb]) === 'function'){
_that = self;
}
if(_that){
return ret = _cb.call(_that, e, elem, ret);
}
})
}
}
Nui.each(events, function(cbs, evts){
if(cbs && (typeof cbs === 'string' || typeof cbs === 'function')){
if(typeof cbs === 'string'){
cbs = Nui.trim(cbs).split(/\s+/);
}
evts = Nui.trim(evts).split(/\s+/);
// keyup:kupdown:focus a => elem.on('keyup kupdown focus', 'a', callback)
evt = evts.shift().replace(/:/g, ' ');
ele = evts.join(' ');
//组件内部处理
if(isComponent){
that._on(evt, elem, ele, function(e, elem){
callback(e, elem, cbs)
})
}
else{
elem.on(evt, ele, function(e){
callback(e, jQuery(this), cbs)
})
}
}
})
return that
}
})