Skip to content

Instantly share code, notes, and snippets.

@apipkin
Forked from lsmith/gist:459293
Created July 1, 2010 19:31
Show Gist options
  • Save apipkin/460436 to your computer and use it in GitHub Desktop.
Save apipkin/460436 to your computer and use it in GitHub Desktop.
YUI.add('gallery-plugin-node-io',function(Y){
var YL = Y.Lang,
SUCCESS = 'success',
FAILURE = 'failure',
TIMEOUT = 'timeout';
Y.Plugin.NodeIo = Y.Base.create('node-io', Y.Base, [], {
_inProgress: null, // verb choice over noun for attribute ?
_ioHandlers: null,
initializer : function(){
this.publish('success', {defaultFn: this._defSuccessFn });
this.publish(FAILURE); // is this needed ?
this.publish(TIMEOUT); // is this needed ?
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; // should this not allow for other states than success, failure, and timeout ?
this._inProgress = Y.io(uri, config);
return this;
},
refresh : this.load,
abort : function() {
this._stopIO();
return this; // better than returning `this` from _stopIO ?
},
_stopIO : function() {
if(this._inProgress) {
this._inProgress.abort(); // should abort be wrapped in a try ?
this._inProgress = null;
}
},
_handleResponse : function (type, id, o) { // type was moved to first argument
this.fire(type, {id: id, response: o});
this._inProgress = null;
},
_defSuccessFn : function(e) {
this.get('host').insert(e.response.responseText, this.get('placement'));
},
_afterUriChange : function() {
this._stopIO(); // doing this on event rather than in setter for e.halt ?
}
}, {
NS : 'io',
ATTRS : {
host : { // borrowed from Y.Plugin.Base. Is this correct ?
writeOnce : true
},
ioConfig : {
value : {},
validator : YL.isObject
},
placement : {
value : 'replace',
validator : function(val) { // regex faster than switch ?
return /replace|(?:ap|pre)pend/.test(val);
}
},
uri : {
validator : YL.isString
}
}
});
},'0.1',{requires:['io','base','node']});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment