Opcija 1
by Osman Salihovic
HTML
<script src="https://code.jquery.com/jquery-2.2.3.js"></script>
<div id="alfagrid1" class="alfagrid" style="border: 1px black solid">
<table>
<thead>
<tr>
<td>Col 1</td>
<td>Col 2</td>
</tr>
</thead>
<tbody>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
</tbody>
</table>
</div>
JavaScript
;(function($, window, document, undefined) {
console.clear();
var pluginName = 'alfagrid';
function Plugin(element, options) {
this.element = element;
this._name = pluginName;
this._defaults = $.fn.alfagrid.defaults;
this.options = $.extend({}, this._defaults, options);
this.init();
}
$.extend(Plugin.prototype, {
init: function() {
this.buildCache();
this.bindEvents();
},
destroy: function() {
this.unbindEvents();
this.$element.removeData();
},
buildCache: function() {
this.$element = $(this.element);
},
bindEvents: function() {
var plugin = this;
plugin.$element.on('click' + '.' + plugin._name, function() {
plugin.someOtherFunction.call(plugin);
});
},
// Unbind events that trigger methods
unbindEvents: function() {
this.$element.off('.' + this._name);
},
// Create custom methods
someOtherFunction: function() {
//console.log($(this).data);
//console.log(this);
this.callback();
},
callback: function() {
// Cache onComplete option
var onComplete = this.options.onComplete;
if (typeof onComplete === 'function') {
onComplete.call(this.element);
}
},
geto: function(){
return this.options;
}
});
$.fn.alfagrid = function(options) {
this.each(function() {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName, new Plugin(this, options));
}
});
return this;
};
$.fn.alfagrid.getoptions = function(options) {
return this;
};
$.fn.alfagrid.defaults = {
property: 'value',
onComplete: null
};
})(jQuery, window, document);
$('#div1').alfagrid({
onComplete: function() {
alert('onComplete callback fired()');
}
});
//console.log($('#div1').data('plugin_alfagrid').geto() );
console.log($('#div1').data('plugin_alfagrid').options);