Skip to content

Instantly share code, notes, and snippets.

@lsmith
Created June 30, 2010 22:13
Show Gist options
  • Save lsmith/459293 to your computer and use it in GitHub Desktop.
Save lsmith/459293 to your computer and use it in GitHub Desktop.
Y.Plugin.NodeIO = Y.Base.create('node-io', Y.Plugin.Base, [], {
_inProgress: null,
_ioHandlers: null,
initializer: function () {
this.publish("success", { defaultFn: this._defSuccessFn });
this.after('uriChange', this._afterUriChange);
this._ioHandlers = {
success: Y.bind(this._handleResponse, this, "success"),
failure: Y.bind(this._handleResponse, this, "failure"),
timeout: Y.bind(this._handleResponse, this, "timeout")
};
},
load : function(uri) {
var config = this.get("ioConfig");
uri || (uri = this.get('uri'));
this.set('uri', uri);
config.on = this._ioHandlers;
this._inProgress = Y.io(this.get('uri'), ioConfig);
return this;
},
abort : function() {
this._stopIO();
return this;
},
_stopIO : function() {
if (this._inProgress) {
this._inProgress.abort();
this._inProgress = null;
}
},
_handleResponse: function (id, o, type) {
this.fire(type, { id: id, response: o });
this._inProgress = null;
},
_defSuccessFn : function(e) {
this.get('host').insert(e.responseText, this.get('placement'));
},
_afterUriChange: function () {
this._stopIO();
}
}, {
NS : 'io',
ATTRS : {
uri : {
validator: Y.Lang.isString
},
placement : {
value : 'replace',
validator : function(val){
return /^(?:replace|(?:ap|pre)pend)$/.test(val);
}
},
ioConfig : {
value: {},
validator: Y.Lang.isObject
}
}
});
// Sugar coated alias
Y.Plugin.NodeIO.prototype.refresh = Y.Plugin.NodeIO.prototype.load;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment