lundi 5 janvier 2015

Adding listeners to elements inside JavaScript module

I learn the design patterns for Javascript modules and I need your advice.


For example I have a js module in one file and onload file. Should I add an elements and listen events like this:



module.js

var Module = (function() {
var obj = {};
var _but;
obj.addElement = function(button, callback) {
_but.onclick = function() { callback() }
}
return obj;
});

init.js

window.onload = function() {
var m = new Module();
m.addElement(document.getElementById("but"), function() {
alert("Do work");
});
}


or should I add events outside the module



module.js

var Module = (function() {
var obj = {};
obj.doWork = function() {
alert("Do work");
}
return obj;
});

init.js

window.onload = function() {
var m = new Module();
var but = document.getElementById("but");
but.onclick = function() {
m.doWork();
}
}


I know this question looks weird, don't blame me, but I need your advice. Thank you.


Aucun commentaire:

Enregistrer un commentaire