PDA

View Full Version : jDataQuery


Sephr
12-20-2008, 03:47 AM
This is a JavaScript snippet for a jData (http://www.eligrey.com/tag/jdata/) querying function with a callback parameter. I have also made a demo (http://code.eligrey.com/jdata/demo1.html) of this function. The code is public domain so modify it as you wish. jData works on all browsers that correctly support localStorage and window.postMessage (IE8 beta 2 has trouble with it so no support currently) so that's why jDataQuery doesn't have any code to support IE8.

var $tempMessageListener, jDataServer = 'http://jdata.eligrey.com',
jDataEnableHash = false; // Set to true to have hash value of jData frame change to current query
var jDataLoadFlags = jDataEnableHash ? '/?hash':'/';
var jDataFrame = document.createElement('iframe');
jDataFrame.setAttribute('src', jDataServer+jDataLoadFlags);
jDataFrame.setAttribute('style', 'display:none');
document.documentElement.appendChild(jDataFrame);

function jDataQuery(query, callback) {
if (callback) { $tempMessageListener = function(e){ getNextjDataResponse(e, callback); } }
else { $tempMessageListener = function(e){ getNextjDataResponse(e); } }
window.addEventListener("message", $tempMessageListener, false);
jDataFrame.contentWindow.postMessage(query, '*');
};

function getNextjDataResponse(e, callback) {
if ( e.origin == jDataServer ) { // Make sure that this is a jData response
window.removeEventListener("message", $tempMessageListener, false);
if (callback) { callback((e.data != 'null' && e.data != 'undefined')?e:undefined); }
$tempMessageListener = undefined;
}
};

Syntax: jDataQuery(query:String [..., callback:Function])

Example:

A website sets the user’s preferred search engine as JSON in preference.searchEngine:

jDataQuery("setItem('preference.searchEngine',\"{'Google':'http://www.google.com/search?q=%s'}\")")

Then another website wants to check out the JSON in preference.searchEngine if it exists:

jDataQuery("getItem('preference.searchEngine')",function(e){setSearchBox(JSON.parse(e.data))})

Sephr
01-14-2009, 06:49 AM
I can no longer edit the above post so I will put this here:
The two examples are now invalid due to a new API.
Make sure to use valid JSON requests that follow the jData API Reference (http://www.eligrey.com/projects/jdata/jdata-api-reference/) when using this function.
The 2nd and 3rd lines should be replaced with var jDataLoadFlags = '/api/postMessage.php'; (and the comma on the first line should be replaced with a semicolon then)

oesxyl
01-14-2009, 08:23 AM
This code is obsolete and is now replaced by JIL. Do not use this code.
Could a mod please delete this thread?
it's a bad idea to delete threads, :)
In my opinion is not obsolete, what if I don't want to use JIL, :)

best regards

Sephr
01-15-2009, 01:37 AM
it's a bad idea to delete threads, :)
In my opinion is not obsolete, what if I don't want to use JIL, :)

best regards

Well at least a mod should delete the 2 examples I have in the first post that use the old insecure API which will no longer work.

oesxyl
01-15-2009, 01:41 AM
Well at least a mod should delete the 2 examples I have in the first post that use the old insecure API which will no longer work.
I guess you can make you this changes and add a notice. Who use this will read all thread, I hope, and see what you change. :)

best regards

Sephr
01-15-2009, 03:59 AM
I guess you can make you this changes and add a notice. Who use this will read all thread, I hope, and see what you change. :)

best regards

It doesn't let me edit posts older than 2 weeks (or something around that amount of time) therefore I can't change it.
Edit: Also, I just noticed this code will not work anymore since it goes to jDataServer+'/' instead of the new API's home at jDataServer+'/api/postMessage.php'

oesxyl
01-15-2009, 04:08 AM
It doesn't let me edit posts older than 2 weeks (or something around that amount of time) therefor I can't change it.
yes I know, this is to not allow people to delete post content, :)
it's my fault, I was not clear in what I said. What I mean is to add a replay where you can make all changes you need. A suggestion, just in case you, if you don't want to maintain this to mention as a notice, :)
I guess, WA or one of the moderators will make the changes for you if don't want to follow my suggestion, but for that you must tell them what you want, :)

best regards