Skip to content

Instantly share code, notes, and snippets.

// Wrapper function to create an img tag and execute a callback when it loads or
// the server returns an error (onload/onerror). Also handles img nodes currently
// on the page, and executes the appropriate handler according to the image's state.
// Mainly useful for insurance in knowing that the dimensions are available at a
// certain point in the processing.
// First a couple helper functions
function $(id) {
return !id || id.nodeType === 1 ? id : document.getElementById(id);
}
new YAHOO.util.XHRDataSource(url, {
responseType: YAHOO.util.DataSource.TYPE_JSON,
responseSchema: {
resultsList: 'Results',
fields: [ 'foo', 'bar', 'baz' ]
}
}).sendRequest(queryString, {
// handler that will execute when the XHR returns
success: function (req, res, payload) {
/* here's where you access the results from res.results */
YAHOO.util.Event.onDOMReady(function () {
var Ex = YAHOO.namespace('example');
Ex.dataSource = new YAHOO.util.DataSource(YOUR_URL,{
responseType : YAHOO.util.DataSource.TYPE_JSON,
responseSchema : {
resultsList: 'records',
fields : ['foo','bar','baz'],
metaFields : { totalRecords: 'total' }
YUI('lang', function (Y) {
YAP.toQueryString = function (obj, name) {
switch (Y.Lang.type(obj)) {
case 'undefined':
case 'null':
return name ? encodeURIComponent(name) + '=' : '';
case 'boolean': obj = +obj; // intentional fallthrough
case 'number':
case 'string':
YUI.add('json-trusted', function (Y) {
function evalWrapper(str) { return eval('('+str+')'); }
Y.namespace('TrustedJSON').parse =
(Object.prototype.toString.call(window.JSON) === '[object JSON]') ?
function (str) {
return /^\s*(?:"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)\s*$/.test(str) ?
evalWrapper(str) : // FF3.5 Native does not support non-object/array input
JSON.parse(str);
function parseISO8601(str) {
return new Date(str.replace(/-/,"/").
replace(/-/,"/").
replace(/T/," ").
replace(/Z/," UTC").
replace(/([\+-]\d\d)\:?(\d\d)/," $1$2")); // -04:00 -> -0400
}
function testFont(name) {
name = name.replace(/['"<>]/g,'');
var body = document.body,
test = document.createElement('div'),
installed = false,
template =
'<b style="display:inline !important; width:auto !important; font:normal 10px/1 \'X\',sans-serif !important">ii</b>'+
'<b style="display:inline !important; width:auto !important; font:normal 10px/1 \'X\',monospace !important">ii</b>',
ab;
YAHOO.my.app._dashboadCallback = function (e) {
alert('subscriber');
YAHOO.my.app.dashboardChangeEvent.unsubscribe(YAHOO.my.app._dashboardCallback);
};
YAHOO.my.app.dashboardChangeEvent.subscribe(YAHOO.my.app._dashboadCallback);
// Function to convert the schema's fields into walk paths
var buildPath = function (needle) {
var path = null, keys = [], i = 0;
if (needle) {
// Strip the ["string keys"] and [1] array indexes
needle = needle.
replace(/\[(['"])(.*?)\1\]/g,
function (x,$1,$2) {
keys[i]=$2;
YAHOO.util.Event.onDOMReady(function () {
var Ex = YAHOO.namespace('example');
YAHOO.util.Connect.asyncRequest('GET','data.php',{
success : function (o) {
var data = YAHOO.lang.JSON.parse(o.responseText);
function dataMarshaller(section) {
return data.records[section] || [];