I coded a dialog factory base like what you are looking for ...
Feel free to ask if you have any question.
Test's fiddle here
// Dialog box factory base
(function ($) {
// kind of private vars
var dialog = function (opt, html) {
this.domElt = $('<div />', {
class: 'dialog-container'
}).append(html);
return this.init(opt);
},dialogsStack = [];
dialog.prototype = {
init: function (options) {
dialogsStack.push(this);
this.params = $.extend(this.params, options, {
dialogsIndex: (dialogsStack.length - 1)
});
console.log(this.params);
},
remove: function (all) {
var dis = this;
this.domElt.remove();
if (dialogsStack.length)
if (all) {
$.each(dialogsStack, function (i, el) {
el.domElt.remove();
// delete el;
});
dialogsStack = [];
} else {
this.domElt.remove();
console.log('rm: ' + this.params.dialogsIndex);
dialogsStack.splice(this.params.dialogsIndex, 1);
}
},
showMe: function () {
this.domElt.appendTo('body');
}
};
$.fn.dialog = function (options) {
var t = this.eq(0);
return new dialog(options, $('<div />').append(t).html());
};
})(jQuery);
this.each?dialog.show();doesn't work.