We will create one object to handle the Global Events, and one object to handle Transaction Events. Each object defines methods to handle the events in a transction's lifecycles.
The results are logged to `
`.
```
//Get a reference to the DIV that we are using
//to report results.
var d = Y.one('#container'),
gStr = '',
tStr = '',
state;
/* global listener object */
var gH = {
write: function(s, args) {
gStr += "ID: " + s;
if (args) {
gStr += " " + "The arguments are: " + args;
}
gStr += "
";
},
start: function(id, args) {
this.write(id + ": Global Event Start.", args);
},
complete: function(id, o, args) {
this.write(id + ": Global Event Complete. The status code is: " + o.status + ".", args);
},
success: function(id, o, args) {
this.write(id + ": Global Event Success. The response is: " + o.responseText + ".", args);
},
failure: function(id, o, args) {
this.write(o + ": Global Event Failure. The status text is: " + o.statusText + ".", args);
},
end: function(id, args) {
this.write(id + ": Global Event End.", args);
if (state === 'global') {
flush(gStr);
}
}
};
/* end global listener object */
/* transaction event object */
var tH = {
write: function(s, args) {
tStr += "ID: " + s;
if (args) {
tStr += " " + "The arguments are: " + args;
}
tStr += "
";
},
start: function(id, args) {
this.write(id + ": Transaction Event Start.", args.start);
},
complete: function(id, o, args) {
this.write(id + ": Transaction Event Complete. The status code is: " + o.status + ".", args.complete);
},
success: function(id, o, args) {
this.write(id + ": Transaction Event Success. The response is: " + o.responseText + ".", args.success);
},
failure: function(id, o, args) {
this.write(id + ": Transaction Event Failure. The status text is: " + o.statusText + ".", args.failure);
},
end: function(id, args) {
this.write(id + ": Transaction Event End.", args.end);
flush(gStr + tStr);
}
};
/* end transaction event object */
/* Output the results to the DIV container */
function flush(s) {
d.set("innerHTML", s);
if (state === 'global') {
gStr = '';
}
else {
gStr = '';
tStr = '';
}
}
```
Subscribe to the Global events
With the handler object gH
```
// Notice the object context of "gH" is provided as the
// third argument of Y.on(), to preserve the proper
// context of 'this' as used in gH's methods.
/* Subscribe to the global events */
Y.on('io:start', gH.start, gH, 'global foo');
Y.on('io:complete', gH.complete, gH, 'global bar');
Y.on('io:success', gH.success, gH, 'global baz');
Y.on('io:failure', gH.failure, gH);
Y.on('io:end', gH.end, gH, 'global boo');
/* End event subscription */
```
Assemble a Configuration Object to set Transaction Event Listeners
Use a configuration object to define which Transaction Events you wish to handle, for the specific transaction.
```
/* Configuration object for setting Transaction Events */
var cfg = {
on: {
start: tH.start,
complete: tH.complete,
success: tH.success,
failure: tH.failure,
end: tH.end
},
context: tH,
arguments: {
start: 'foo',
complete: 'bar',
success: 'baz',
failure: 'Oh no!',
end: 'boo'
}
};
```
Initiate the Transaction
Finally, we set up two buttons -- one for each type of transaction -- and add a "click" listener to each of them. The handler -- function call() -- make an
IO request, based on which button was clicked.
```
function call(e, b) {
if (b) {
state = 'all';
}
else {
state = 'global';
}
Y.io('{{componentAssets}}/get.txt', cfg);
}
Y.on('click', call, "#get1", this, false);
Y.on('click', call, "#get2", this, true);
```
Full Code
The full JavaScript code for this example follows:
```
{{>io-get-source}}
```