Skip to content

Instantly share code, notes, and snippets.

@phiggins42
Created February 18, 2011 16:39
Show Gist options
  • Save phiggins42/833948 to your computer and use it in GitHub Desktop.
Save phiggins42/833948 to your computer and use it in GitHub Desktop.
dojo.NodeList.prototype.grab = function(url, extraArgs, method){
// summary: Grab some remote HTML and inject into these nodes.
//
// url: String
// A url to fetch. Uses `dojo.xhr`, so must be same domain
//
// extraArgs: dojo._ioArgs?
// An Optional parameter to mix in other standard dojo._ioArgs
// into this request. (such as sync, timeout, error callbacks,
// and so on.)
//
// method: String?
// An optional HTTP verb to use. defaults to `GET`. Defines the transport
// to use, like POST/PUT/DELETE, and so on.
//
// example:
// | dojo.query("#header").grab("header.html");
//
// example:
// By using extraArgs we can make this NodeList xhr action synchronous
// so the following chained functions apply to the newly injected markup.
// | dojo.query("ul li").grab("listitem.html", { sync:true })
// | .query("a").onclick(function(e){ dojo.stopEvent(e); });
//
// example:
// An extraArgs load: function can be used to manipulate the data before
// it is injected into the node. Simply return valid HTML from the callback.
// |
// | dojo.query("ul#bar").grab("foo.json", {
// | handleAs:"json",
// | load:function(response){
// | return dojo.replace("<li>{title}</li>", response);
// | }
// | });
this.length && dojo.xhr(method || "GET", dojo._mixin({ url:url }, extraArgs))
.addCallback(this, function(r){ this.addContent(r, "only"); });
return this; // dojo.NodeList
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment