At every project I'm asking myself which solution is better if I need multiple instances of an module.
1: Revealing module pattern:
var Module = (function () {
var init = function () {
$('[data-slider]').each(function() {
$(this).slider();
remove($(this));
});
};
// always need current element
var remove = function($this) {
$this.remove();
};
return {
init: init
};
})();
Module.init();
2: Real multiple instances:
var Module = function () {
var options;
var init = function (data) {
options = data;
options.$this.slider();
remove();
};
// $this is always stored in options
var remove = function() {
options.$this.remove();
};
return {
init: init
};
};
$('[data-slider]').each(function() {
new Module().init({
$this: $(this)
});
});
In my opinion the second solution is much "cleaner" but I almost always see the first solution. Or is there something better for doing this? What are you using?
Aucun commentaire:
Enregistrer un commentaire