summaryrefslogtreecommitdiff
path: root/lib/dojo/request/notify.js.uncompressed.js
diff options
context:
space:
mode:
authorRichard Beales <[email protected]>2013-03-18 07:32:01 +0000
committerRichard Beales <[email protected]>2013-03-18 07:32:01 +0000
commit7c97d17aaf373339a8bcd917ad59ca6018148f0d (patch)
tree5a3c04f0f9529be392c1263d3feb75806eb43797 /lib/dojo/request/notify.js.uncompressed.js
parent70db7424e7068701e60cc5bcdfe8f858be508179 (diff)
parentc670a80ddd9b03bd4ea6d940a9ed682fd26248d7 (diff)
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'lib/dojo/request/notify.js.uncompressed.js')
-rw-r--r--lib/dojo/request/notify.js.uncompressed.js74
1 files changed, 74 insertions, 0 deletions
diff --git a/lib/dojo/request/notify.js.uncompressed.js b/lib/dojo/request/notify.js.uncompressed.js
new file mode 100644
index 000000000..2b8a2b68b
--- /dev/null
+++ b/lib/dojo/request/notify.js.uncompressed.js
@@ -0,0 +1,74 @@
+define("dojo/request/notify", ['../Evented', '../_base/lang', './util'], function(Evented, lang, util){
+ // module:
+ // dojo/request/notify
+ // summary:
+ // Global notification API for dojo/request. Notifications will
+ // only be emitted if this module is required.
+ //
+ // | require('dojo/request', 'dojo/request/notify',
+ // | function(request, notify){
+ // | notify('load', function(response){
+ // | if(response.url === 'someUrl.html'){
+ // | console.log('Loaded!');
+ // | }
+ // | });
+ // | request.get('someUrl.html');
+ // | }
+ // | );
+
+ var pubCount = 0,
+ slice = [].slice;
+
+ var hub = lang.mixin(new Evented, {
+ onsend: function(data){
+ if(!pubCount){
+ this.emit('start');
+ }
+ pubCount++;
+ },
+ _onload: function(data){
+ this.emit('done', data);
+ },
+ _onerror: function(data){
+ this.emit('done', data);
+ },
+ _ondone: function(data){
+ if(--pubCount <= 0){
+ pubCount = 0;
+ this.emit('stop');
+ }
+ },
+ emit: function(type, event){
+ var result = Evented.prototype.emit.apply(this, arguments);
+
+ // After all event handlers have run, run _on* handler
+ if(this['_on' + type]){
+ this['_on' + type].apply(this, slice.call(arguments, 1));
+ }
+ return result;
+ }
+ });
+
+ function notify(type, listener){
+ // summary:
+ // Register a listener to be notified when an event
+ // in dojo/request happens.
+ // type: String?
+ // The event to listen for. Events emitted: "start", "send",
+ // "load", "error", "done", "stop".
+ // listener: Function?
+ // A callback to be run when an event happens.
+ // returns:
+ // A signal object that can be used to cancel the listener.
+ // If remove() is called on this signal object, it will
+ // stop the listener from being executed.
+ return hub.on(type, listener);
+ }
+ notify.emit = function(type, event, cancel){
+ return hub.emit(type, event, cancel);
+ };
+
+ // Attach notify to dojo/request/util to avoid
+ // try{ require('./notify'); }catch(e){}
+ return util.notify = notify;
+});