Commit 42bdbc93 authored by Thomas Lechauve's avatar Thomas Lechauve

Fix: keeping context in ajax request

Also modified all tests to use slapos plugin new structure
parent 674bb1bc
(function($) { ;(function($) {
var methods = { var methods = {
defaults: { defaults: {
host: '' host: ''
}, },
init: function( options ){ init: function( options ){
var settings = $.extend({
'host': ''
}, options);
return this.each(function(){ return this.each(function(){
methods.config = $.extend({}, methods.defaults, methods.options, methods.metadata);
methods['store'] = Modernizr.localstorage ? methods.lStore : methods.cStore; methods['store'] = Modernizr.localstorage ? methods.lStore : methods.cStore;
methods.store('host', methods.config.host); $(this).slapos('store', 'host', settings.host);
}); });
}, },
...@@ -41,67 +44,68 @@ ...@@ -41,67 +44,68 @@
} }
}, },
request: function(context, type, url, callback, statusEvent, data){ request: function(type, url, callback, statusEvent, data){
data = data || ''; data = data || '';
statusEvent = statusEvent || this.statusDefault; statusEvent = statusEvent || this.statusDefault;
$.ajax({ return this.each(function(){
url: methods.store('host')+url, $.ajax({
dataType: 'json', url: methods.store('host')+url,
data: data, dataType: 'json',
context: context, data: data,
type: type, context: $(this),
statusCode: statusEvent type: type,
}).done(callback); statusCode: statusEvent,
success: callback
});
});
}, },
newInstance: function(data, callback, statusEvent){ newInstance: function(data, callback, statusEvent){
return this.request('POST', '/request', callback, statusEvent, data); return $(this).slapos('request', 'POST', '/request', callback, statusEvent, data);
}, },
deleteInstance: function(id, callback, statusEvent){ deleteInstance: function(id, callback, statusEvent){
return this.request('DELETE', '/instance/'+id, callback, statusEvent); return $(this).slapos('request', 'DELETE', '/instance/'+id, callback, statusEvent);
}, },
getInstance: function(id, callback, statusEvent){ getInstance: function(id, callback, statusEvent){
return this.each(function(){ return $(this).slapos('request', 'GET', '/instance/'+id, callback, statusEvent);
methods.request(this, 'GET', '/instance/'+id, callback, statusEvent);
});
}, },
getInstanceCert: function(id, callback, statusEvent){ getInstanceCert: function(id, callback, statusEvent){
return this.request('GET', '/instance/'+id+'/certificate', callback, statusEvent); return $(this).slapos('request', 'GET', '/instance/'+id+'/certificate', callback, statusEvent);
}, },
bangInstance: function(id, log, callback, statusEvent){ bangInstance: function(id, log, callback, statusEvent){
return this.request('POST', '/instance/'+id+'/bang', callback, statusEvent, log); return $(this).slapos('request', 'POST', '/instance/'+id+'/bang', callback, statusEvent, log);
}, },
editInstance: function(id, data, callback, statusEvent){ editInstance: function(id, data, callback, statusEvent){
return this.request('PUT', '/instance/'+id, callback, statusEvent, data); return $(this).slapos('request', 'PUT', '/instance/'+id, callback, statusEvent, data);
}, },
newComputer: function(data, callback, statusEvent){ newComputer: function(data, callback, statusEvent){
return this.request('POST', '/computer', callback, statusEvent, data); return $(this).slapos('request', 'POST', '/computer', callback, statusEvent, data);
}, },
getComputer: function(id, callback, statusEvent){ getComputer: function(id, callback, statusEvent){
return this.request('GET', '/computer/'+id, callback, statusEvent); return $(this).slapos('request', 'GET', '/computer/'+id, callback, statusEvent);
}, },
editComputer: function(id, data, callback, statusEvent){ editComputer: function(id, data, callback, statusEvent){
return this.request('PUT', '/computer/'+id, callback, statusEvent, data); return $(this).slapos('request', 'PUT', '/computer/'+id, callback, statusEvent, data);
}, },
newSoftware: function(computerId, data, callback, statusEvent){ newSoftware: function(computerId, data, callback, statusEvent){
return this.request('POST', '/computer/'+computerId+'/supply', callback, statusEvent, data); return $(this).slapos('request', 'POST', '/computer/'+computerId+'/supply', callback, statusEvent, data);
}, },
bangComputer: function(id, log, callback, statusEvent){ bangComputer: function(id, log, callback, statusEvent){
return this.request('POST', '/computer/'+id+'/bang', callback, statusEvent, log); return $(this).slapos('request', 'POST', '/computer/'+id+'/bang', callback, statusEvent, log);
}, },
computerReport: function(id, data, callback, statusEvent){ computerReport: function(id, data, callback, statusEvent){
return this.request('POST', '/computer/'+id+'/report', callback, statusEvent, data); return $(this).slapos('request', 'POST', '/computer/'+id+'/report', callback, statusEvent, data);
} }
}; };
...@@ -114,5 +118,4 @@ ...@@ -114,5 +118,4 @@
$.error( 'Method ' + method + ' does not exist on jQuery.slapos' ); $.error( 'Method ' + method + ' does not exist on jQuery.slapos' );
} }
}; };
})(jQuery); })(jQuery);
\ No newline at end of file
This diff is collapsed.
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