I've been trying to build the jquery equivalent of a class and methods in order to send ajax information back to CodeIgniter or anything else that might need it.
I'm totally new to Jquery really and I've got the ajax code working in the past but not as a reusable component. The class (if that's the right word) here has:
send_data(options) method
_required(required, data) method to check if required options have been sent to send_data()
_default(defaults,options) to set default values if certain options aren't set.
Here is my code. Be warned that a lot of this code could be pure fantasy because I'm new to this and I'm being bold. I suppose the question is, is the code right?
- jQuery.fn.exists = function(){return this.length>0;}//Add an exists function for ease of use.
/**
* Ajax Class
*
* @package ajax
* @author not in pastebin
*/
var Data_transfer = function()
{
//Get cwd.
this.loc = window.location.pathname;
this.dir = loc.substring(0, loc.lastIndexOf('/'));//From pos 0 to last "/" gets the directory and not filename.
/**
* Send data ajax method. Provide object with: data:{params}, folder, method='POST', data_type='json', login_folder=this.dir, cookie_name
*
* @access public
* @param object array
* @return string Returns string containing result information / error or false on failure.
*/
this.send_data = function(options)
{
if(!this._required({options.data,options.folder},options))
return {error:"Required options missing."};
var default_list = {folder:this.dir,
method:'POST',
data_type:'JSON',
login_folder='http://'+document.location.hostname,
cookie_name:"csrf_simple_cookie"};//List of options which are set if not found.
//JQUERY FORUM: Is it ok to overrite with options = options?
var options = this._default(default_list,options);//Set the defaults for the options coming in.
var cct = $.cookie(options.cookie_name);//Overcome ajax limitation with csrf function in CodeIgniter. It passes back the csrf code to CodeIgniter allowing form to be sent.
$.ajax({
url: options.folder,
type: options.method.toUpperCase(),
context: document.body,
data: {options.data},
dataType: data_type.toLowerCase(),
error: function(XMLHttpRequest,error)
{
if(error=='parsererror')//Not logged in.
{
window.location.replace(options.login_folder)//redirect to login page
}
},
success: function(data)
{
if(data.success)
return data.result;
else
return data.error;
}
return false;//Unknown?
});
}
/** Utility Methods **/
/**
* _required Method checks the given array of field data aggainst array of required fields.
*
* @access public
* @param object array
* @return array
*/
this._required=function(required, data)
{
required.each(required,function(key,value)
{
if(!data[value].exists())
return false;
});
return true;
}
/**
* _default method combines the defaults with options passed to original method.
*
* @access public
* @param object array
* @return array
*/
this._default=function(defaults,options)
{
return $.merge(defaults,options);
}
}
//Send data ajax method. Provide object with: data:{params}, folder, method='POST', data_type='json', login_folder=this.dir, cookie_name (optional)
var my_fields = {example:"This is a value"};
var options = {data:my_fields,
folder:this.dir+"/tasks/get_projects_ajax",
method:"POST",
data_type:"json",
login_folder:this.dir+"/login"};
var dt = new Data_transfer();
var returned = dt.send_data(options);
if(returned.error.exists())
{
console.log(returned.error);
}
else if(returned.result.exists())
{
console.log(returned.result);
}
else
{
console.log("Method failed");
}
Edited:
Actually I think I need to understand the required function first, that code defintiely needs work. I've changed it but for some reason it returns the wrong result.
- this._required=function(required, data)
- {
- console.log("required ran");
- $.each( required, function(key,value){
- if(!(value.toString() in data))
- {
- return false;
- }
- console.log("value to string = "+value.toString());
- console.log("Key="+key+", value="+value);
- console.log(data);
- });
- }
- this.send_data = function(options)
{
if(this._required({a:"mydata"},{mydata:"thing"}))
console.log("true");
else
console.log("false"); - }
- var dt = new Data_transfer();
var returned = dt.send_data({a:"thing"});