Skip to content

Instantly share code, notes, and snippets.

@skamenetskiy
Last active December 22, 2015 00:29
Show Gist options
  • Save skamenetskiy/6390083 to your computer and use it in GitHub Desktop.
Save skamenetskiy/6390083 to your computer and use it in GitHub Desktop.
/**
* User: Semen Kamenetskiy
* Date: 30.08.13
*/
!function () {
'use strict';
/**
* Global object
* @type {window|*}
*/
var root = window || this;
/**
* A void function
* @private
*/
function __void() {
}
/**
* Extending XMLHttpRequest, appending callback methods
* @private
*/
root['XMLHttpRequest'].prototype['__JSONObjectCallbacks'] = {
success: __void,
error: __void
};
/**
* onReadyStateChange event handler
* @private
*/
function __onReadyStateChange() {
if (this.readyState == 4) {
if (this.status == 200) {
this['__JSONObjectCallbacks'].success(this.responseText);
} else {
this['__JSONObjectCallbacks'].error(this.status);
}
}
}
/**
* Converts object to url-friendly string
* @param {Object} params
* @returns {string}
* @private
*/
function __parseRequestParams(params) {
if ('object' != typeof(params)) {
throw new JSONObjectError('params is undefined or not an object');
}
var requestStringArray = [];
for (var param in params) {
if (params.hasOwnProperty(param)) {
requestStringArray.push(param + '=' + encodeURIComponent(params[param]));
}
}
return requestStringArray.join('&');
}
/**
* Custom error class
* @param message
* @constructor
*/
function JSONObjectError(message) {
this.name = 'JSONObjectError';
this.message = message || 'Unknown JSONObject error';
}
/**
* Extending Error prototype
* @type {*}
*/
JSONObjectError.prototype = root['Error'].prototype;
function JSONObject() {
this.onreadystatechange = __onReadyStateChange;
}
JSONObject.prototype = {
/**
* XMLHttpRequest container
*/
request: new root.XMLHttpRequest(),
/**
* XMLHttpRequest url
*/
requestUrl: undefined,
/**
* XMLHttpRequest async
*/
requestAsync: false,
/**
* XMLHttpRequest method
*/
requestMethod: 'GET',
syncInterval: null,
/**
* Load data from server
* @param {String} [url] Url to make request from
* @return {JSONObject}
*/
'load': function (url, params) {
if ('string' != typeof this.url) {
throw new JSONObjectError('url is undefined or not a string');
}
this
.request
.open(this.method, this.url, true);
this
.request
.send(params || {});
return this;
},
/**
*
* @param time
*/
'setSyncInterval': function (time) {
this.syncInterval = time || null;
},
'setSyncMethod': function (method) {
this.requestMethod = method || 'GET';
return this;
},
'setSyncUrl': function (url) {
this.requestUrl = url || null;
return this;
},
'onBeforeChange': function () {
},
'onChange': function () {
}
};
root['JSONObject'] = JSONObject;
}();

JSONObject

Small library to handle asynchronously updated JSON data

copyright

2013 Semen Kamenetskiy

E-mail. [email protected]

WWW. http://skamenetskiy.me

license

This library is licensed under the MIT license; you can find a full copy of the license itself in the file /LICENSE

Copyright (c) 2013 Semen Kamenetskiy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@skamenetskiy
Copy link
Author

Just a scratch... :)

Don't take this gist seriously and surely don't use it for production. This was the simplest way to transfer code from work to home.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment