Skip to content

Instantly share code, notes, and snippets.

@jshirley
Created May 29, 2009 03:30
Show Gist options
  • Save jshirley/119763 to your computer and use it in GitHub Desktop.
Save jshirley/119763 to your computer and use it in GitHub Desktop.
YUI().use("widget", "overlay", "node", "io-queue", function(Y) {
var overlay = new Y.Overlay({
// width: "10em",
// height: "10em",
headerContent: "",
bodyContent: "This will be replaced",
zIndex: 500,
visible: false
});
overlay.render();
var RestEventHandler = {
start: function(id, args) {
},
complete: function(id, o, args) {
},
completeAndReplace: function(id, o, args) {
var tgt = args[0];
var p = tgt.get('parentNode');
if ( !p ) return;
p.set('innerHTML', o.responseText);
},
completeAndPopup: function(id, o, args) {
var tgt = args[0];
overlay.set('bodyContent', o.responseText);
overlay.show();
},
failure: function(id, o, args) {
alert("Darn: " + o.status);
},
abort: function(id, args) {
}
};
Y.on('click', function(e) {
var tgt = e.target;
/* An href element with the rest-delete class */
if ( tgt.get('tagName') === 'A' &&
( tgt.hasClass('rest-delete') || tgt.hasClass('rest-post') ||
tgt.hasClass('rest-put') || tgt.hasClass('rest-get') )
) {
e.halt();
var uri = tgt.get('href');
var cfg = {
headers: { 'Content-Type': 'text/html; charset=utf-8' }
};
if ( tgt.hasClass('rest-delete') )
cfg.method = 'DELETE';
else if ( tgt.hasClass('rest-post') )
cfg.method = 'POST';
else if ( tgt.hasClass('rest-put') )
cfg.method = 'PUT';
else if ( tgt.hasClass('rest-get') )
cfg.method = 'GET';
/* On complete, delete the parent element */
var extra = [];
var completeHandler = RestEventHandler.complete;
if ( tgt.hasClass('delete-parent') ) {
}
else if ( tgt.hasClass('replace-parent') ) {
extra.push('frontend-replace-parent=1');
completeHandler = RestEventHandler.completeAndReplace;
}
else if ( tgt.hasClass('popup-response') ) {
extra.push('frontend-popup-response=1');
completeHandler = RestEventHandler.completeAndPopup;
overlay.set("align", { node: tgt, points:[Y.WidgetPositionExt.TR
, Y.WidgetPositionExt.TR]});
}
if ( uri.indexOf('?') > 0 )
uri += '&' + extra.join('&');
else
uri += '?' + extra.join('&');
Y.on('io:start', RestEventHandler.start, this, [ tgt ]);
Y.on('io:complete', completeHandler, this, [ tgt ]);
Y.on('io:abort', RestEventHandler.abort, this, [ tgt ]);
Y.on('io:failure', RestEventHandler.failure, this, [ tgt ]);
var request = Y.io(uri, cfg);
}
}, document.body);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment