Commit 674bb1bc authored by Thomas Lechauve's avatar Thomas Lechauve

Upgrade plugin structure to maintain chaining

parent 540e5b0b
;(function($, window, document, undefined) { (function($) {
var SlapOs = function(elem, options){ var methods = {
this.elem = elem;
this.$elem = $(elem);
this.options = options;
this.metadata = this.$elem.data('plugin-options');
};
SlapOs.prototype = {
defaults: { defaults: {
host: '' host: ''
}, },
init: function(){ init: function( options ){
this.config = $.extend({}, this.defaults, this.options, this.metadata); return this.each(function(){
this.store = Modernizr.localstorage ? this.lStore : this.cStore; methods.config = $.extend({}, methods.defaults, methods.options, methods.metadata);
this.store('host', this.config.host); methods['store'] = Modernizr.localstorage ? methods.lStore : methods.cStore;
return this; methods.store('host', methods.config.host);
});
}, },
/* Local storage method */ /* Local storage method */
...@@ -47,14 +41,14 @@ ...@@ -47,14 +41,14 @@
} }
}, },
request: function(type, url, callback, statusEvent, data){ request: function(context, type, url, callback, statusEvent, data){
data = data || ''; data = data || '';
statusEvent = statusEvent || this.statusDefault; statusEvent = statusEvent || this.statusDefault;
return $.ajax({ $.ajax({
url: this.store('host')+url, url: methods.store('host')+url,
dataType: 'json', dataType: 'json',
data: data, data: data,
context: this.$elem, context: context,
type: type, type: type,
statusCode: statusEvent statusCode: statusEvent
}).done(callback); }).done(callback);
...@@ -69,7 +63,9 @@ ...@@ -69,7 +63,9 @@
}, },
getInstance: function(id, callback, statusEvent){ getInstance: function(id, callback, statusEvent){
return this.request('GET', '/instance/'+id, callback, statusEvent); return this.each(function(){
methods.request(this, 'GET', '/instance/'+id, callback, statusEvent);
});
}, },
getInstanceCert: function(id, callback, statusEvent){ getInstanceCert: function(id, callback, statusEvent){
...@@ -109,12 +105,14 @@ ...@@ -109,12 +105,14 @@
} }
}; };
$.fn.slapos = function(options){ $.fn.slapos = function(method){
return this.each(function(){ if ( methods[method] ) {
new SlapOs(this, options).init(); return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
}); } else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.slapos' );
}
}; };
window.SlapOs = SlapOs; })(jQuery);
\ No newline at end of file
})(jQuery, window , document);
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment