1 //require <jquery.packed.js>
  2 //require <xatajax.core.js>
  3 (function(){
  4 	var $ = jQuery;
  5 	var xataface = XataJax.load('xataface');
  6 	xataface.IO = IO;
  7 	xataface.IO.update = update;
  8 	xataface.IO.insert = insert;
  9 	xataface.IO.load = load;
 10 	
 11 	
 12 	/**
 13 	 * @class
 14 	 * @name IO
 15 	 * @memberOf xataface
 16 	 * @description Just a placeholder class.  Most functions are static.
 17 	 * @example
 18 	 * //require <xataface/IO.js>
 19 	 * var IO = XataJax.load('xataface.IO');
 20 	 * var recordId = 'people?person_id=10';
 21 	 * IO.load(recordId, function(res){
 22 	 * 		alert("Person was loaded: "+res.first_name);
 23 	 * 		// Now let's make a change and save them
 24 	 *      var newVals = {
 25 	 * 			first_name: res.first_name+' changed'
 26 	 * 		};
 27 	 * 		IO.update(recordId, newVals, function(updateRes){
 28 	 * 			if ( updateRes.code == 200 ){
 29 	 * 				alert('Successfully saved changes');
 30 	 *			} else {
 31 	 *				alert("Failed: "+updateRes.message);
 32 	 *			}
 33 	 *		});
 34 	 *
 35 	 * });
 36 	 */
 37 	function IO(/**Object*/ params){
 38 	
 39 	
 40 	}
 41 	
 42 	/**
 43 	 * @name UpdateCallback
 44 	 * @memberOf xataface.IO
 45 	 * @function
 46 	 *
 47 	 * @description A callback function to be passed as the callback to xataface.IO.save
 48 	 *
 49 	 * @param {Object} param
 50 	 * @param {int} param.code The response code.  200 for success.  Anything else for failure.
 51 	 * @param {String} param.message The message indicating what happened.  A server status message.
 52 	 * @param {String} param.recordId The ID of the record that was updated.  (Only included upon
 53 	 *		successful update.
 54 	 *
 55 	 * @see xataface.IO.update
 56 	 */
 57 	
 58 	
 59 	/**
 60 	 * @name update
 61 	 * @function
 62 	 * @memberOf xataface.IO
 63 	 *
 64 	 * @description Updates a record's values in the database.
 65 	 * @param {String} recordId The Xataface Record ID of the record to update.
 66 	 * @param {Object} vals Key-value pairs of the data to update in the record.
 67 	 * @param {xataface.IO.UpdateCallback} callback A callback function to call on completion.
 68 	 * @returns {void}
 69 	 */
 70 	function update(/**String*/ recordId, /**Object*/ vals, /**Function*/ callback){
 71 		if ( typeof(callback) == 'undefined' ) callback = function(){};
 72 		
 73 		var q = $.extend({
 74 			'-action': 'ajax_save',
 75 			'--record_id': recordId
 76 			
 77 			},
 78 			vals
 79 		);
 80 		
 81 		$.post(DATAFACE_SITE_HREF, q, callback);
 82 			
 83 	}
 84 	
 85 	
 86 	/**
 87 	 * @name InsertCallback
 88 	 * @memberOf xataface.IO
 89 	 * @function
 90 	 *
 91 	 * @description A callback function to be passed as the callback to xataface.IO.save
 92 	 *
 93 	 * @param {Object} param
 94 	 * @param {int} param.code The response code.  200 for success.  Anything else for failure.
 95 	 * @param {String} param.message The message indicating what happened.  A server status message.
 96 	 * @param {String} param.recordId The ID of the record that was updated.  (Only included upon
 97 	 *		successful update.
 98 	 *
 99 	 * @see xataface.IO.insert
100 	 */
101 	
102 	
103 	/**
104 	 * @name insert
105 	 * @function
106 	 * @memberOf xataface.IO
107 	 *
108 	 * @description Inserts a record into the database.
109 	 * @param {String} table The name of the table to insert into.
110 	 * @param {Object} vals Key-value pairs of the data to insert in the record.
111 	 * @param {xataface.IO.InsertCallback} callback A callback function to call on completion.
112 	 * @returns {void}
113 	 */
114 	function insert(/**String*/ table, /**Object*/ vals, /**Function*/ callback){
115 		if ( typeof(callback) == 'undefined' ) callback = function(){};
116 		
117 		var q = $.extend({
118 			'-action': 'ajax_insert',
119 			'-table': table
120 			
121 			},
122 			vals
123 		);
124 		
125 		$.post(DATAFACE_SITE_HREF, q, callback);
126 		
127 	
128 	}
129 	
130 	
131 	/**
132 	 * @function
133 	 * @name LoadCallback
134 	 * @memberOf xataface.IO
135 	 * @description Callback function format that can be passed to the xataface.IO.load() function.
136 	 * @param {Object} record The record that was retrieved.
137 	 * @param {String} record.<fieldname> The value of a field in the record.
138 	 *
139 	 * @see The <a href="http://xataface.com/dox/core/latest/classdataface__actions__export__json.html">dataface_actions_export_json</a>
140 	 *	action for the format of the response.
141 	 */
142 	
143 	
144 	/**
145 	 * @function
146 	 * @name load
147 	 * @memberOf xataface.IO
148 	 * @description Loads a record via AJAX using Xataface query conventions.
149 	 *
150 	 * @param {Object} query The Xataface query to specify which record to load.
151 	 * @param {xataface.IO.LoadCallback} callback The callback function to call when loading
152 	 *  is complete.
153 	 */
154 	function load(/**Object*/query, /**Function*/callback){
155 		if ( typeof(query) == 'String' ){
156 			query = {
157 				'--selected-ids': query
158 			};
159 		}
160 		
161 		$.extend(query, {
162 			'-action': 'export_json',
163 			'-mode': 'browse'
164 		});
165 		
166 		$.get(DATAFACE_SITE_HREF, query, function(res){
167 			callback.call(res);
168 			
169 		});
170 	
171 	}
172 	
173 	
174 	
175 
176 })();