-
-
Save lsmith/460684 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ? | |
// A: inProgress is a boolean-esque name and more descriptive | |
_ioHandlers: null, | |
initializer : function(){ | |
this.publish(SUCCESS, {defaultFn: this._defSuccessFn }); | |
//this.publish(FAILURE); // is this needed ? | |
//this.publish(TIMEOUT); // is this needed ? | |
// A: nope | |
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 ? | |
// A: I think the only other one is complete, | |
// but sure. | |
this._inProgress = Y.io(uri, config); | |
return this; | |
}, | |
refresh : this.load, // 'this' is the global object here, boom | |
abort : function() { | |
this._stopIO(); | |
return this; // better than returning `this` from _stopIO ? | |
// A: Prefer returning this from public APIs. Internal | |
// utility methods needn't have the sugar, but it's harmless | |
// either way. | |
}, | |
_stopIO : function() { | |
if(this._inProgress) { | |
this._inProgress.abort(); // should abort be wrapped in a try ? | |
// A: does it throw? If so, yes. | |
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 ? | |
// setter for value munging, events for side effects | |
} | |
}, { | |
NS : 'io', | |
ATTRS : { | |
host : { // borrowed from Y.Plugin.Base. Is this correct ? | |
// A: yep. I don't do writeOnce, but that's prob a good idea | |
writeOnce : true | |
}, | |
ioConfig : { | |
value : {}, | |
validator : YL.isObject | |
}, | |
placement : { | |
value : 'replace', | |
validator : function(val) { // regex faster than switch ? | |
// A: No. Smaller. This isn't a perf vector | |
return /replace|(?:ap|pre)pend/.test(val); | |
} | |
}, | |
uri : { | |
validator : YL.isString | |
} | |
} | |
}); | |
},'0.1',{requires:['io-base','base','node']}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment