/** * @desc Allows you to retrive remote data is JSON format. Part of the Codereck Javascript Suite. * * @copyright Ryan Zec 2007-2009, All Right Reserved * * @license BSD (see LICENSE file included, the generic templete can be found at http://www.opensource.org/licenses/bsd-license.php) */ (function($) { //create the codereck scope $.cr = $.cr || {}; //codereck form validation $.cr.remote_data = function(options) { var self = this; self.data = {}; self.count = 0; self.options = $.extend( { //URL to request from the AJAX call url: null, //whether or not the AJAX call is asynchronous async: false, //send the request through get or post send_type: 'post', //these are extra parameters that are passed to the ajax call extra_data: { start: 0, stop: 0 }, //the index where the data is store data_index: 'data', //this index where the count of the number of records is stored count_index: 'count', // function to if data was successful loaded callback: function(){} }, options); var options = self.options; $.ajax( { url: options.url, async: options.async, data: options.extra_data, type: options.send_type, success: function(results) { //eval('var results = ' + results); eval("var results = (" + results + ")"); //eval('(' + req.responseText + ')'); self.data = results[options.data_index]; self.count = results[options.count_index]; options.callback(self); } }); return self; } $.extend($.cr.remote_data.prototype, $.cr.core); $.extend($.cr.remote_data.prototype, { /** * @desc Return the number of records returned * @return Int Records returned count */ get_count: function() { return this.count; }, /** * @desc Allows you to perform and function on each record returned. The function take 2 parameters, the first is the key and the second is the value(this uses jQuery $.each) * @param function Looping function * @return void */ each: function(loop) { var self = this; $.each(self.data, loop); }, /** * @desc Returns a record based on passed parameter. * @param int Record index to return * @return json Record */ get_record: function(index) { index--; return this.data[index]; }, /** * @desc Returns a record based on passed parameter. * @param int Record id to return * @return json Record */ get_record_by_id: function(id) { }, /** * @desc Add a record to the end of the data aset. * @param int Record id to return(0 starting base) * @return json Record */ add_record: function(data) { var new_index = this.data.length; this.data[new_index] = data; this.count++; } }); })(jQuery);