1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| <div class="test1" lay-filter="data1"></div> <div class="test2" lay-filter="data2"></div>
<script> (function(window) { function InitClass(options) { this.options=options; this.filter=document.querySelector(options.el).getAttribute("lay-filter"); this.render(); } InitClass.prototype.render=function() { document.querySelector(this.options.el).innerHTML='<input type="text"><button>获取</button>'; this.evt(); } InitClass.prototype.evt=function() { var that=this; var childs=document.querySelector(this.options.el).children; childs[1].onclick=function() { var val=childs[0].value; if(val && !isNaN(val)){ InitClass.event.call(this, "testModule", 'getNumberInp('+that.filter+')', { value: Number(val) }); } } }
InitClass.config={ event: {} } InitClass.onevent=function(modName, events, callback) { if (typeof modName !== 'string' || typeof callback !== 'function') return this; return InitClass.event(modName, events, null, callback); } InitClass.event=function(modName, events, params, fn) { var that = this ,filter = events.match(/\((.*)\)$/) || [] ,eventName = (modName + '.' + events).replace(filter[0], '') ,filterName = filter[1] || '';
if (fn) { InitClass.config.event[eventName] = InitClass.config.event[eventName] || {}; InitClass.config.event[eventName][filterName] = fn; return this; }
for(var key in InitClass.config.event[eventName]){ key === filterName && InitClass.config.event[eventName][key].call(that, params); } }
var testModule = { on: function(events, callback) { return InitClass.onevent.call(this, 'testModule', events, callback); }, render: function(options) { new InitClass(options); } } window.testModule=testModule; })(window)
testModule.render({ el: ".test1" }); testModule.render({ el: ".test2" });
testModule.on("getNumberInp(data1)", function(d) { console.log("第一个对象返回的数据:"+d.value) }) testModule.on("getNumberInp(data2)", function(d) { console.log("第二个对象返回的数据:"+d.value) }) </script>
|