do.html revision 1b298c6f0ef597aa4ab0b8bcb25430b6c9a87749
<html>
<head>
<title>YUI 3.0</title>
<script type="text/javascript">
(function() {
var Y = YUI().use('aop', 'dump');
function TestObj(name) {
if (name) {
this.name = name;
}
}
TestObj.prototype.printName = function () {
return true;
};
function bef(msg, obj, a3) {
// Y.log("before " + msg + ", scope: " + obj.testProp);
// Y.log(Y.Lang.dump(a3));
// Y.log('bef: ' + Y.Lang.dump(arguments));
}
function TestScope(name) {
this.testProp = name;
}
function alterArgs(msg, obj) {
var msg = Y.log(msg = "altered args!");
return ( new Y.Do.AlterArgs(msg, ["newarg"]) );
}
function causeError(msg, obj) {
var msg = Y.log(msg = "ack!");
return ( new Y.Do.Error(msg, false) );
}
function aft(msg, ret, obj) {
// Y.log("after " + msg + ", scope: " + obj.testProp);
// Y.log('after: ' + Y.Lang.dump(arguments));
}
function alterReturn(msg, ret, obj) {
var msg = Y.log(msg = "new return val!");
return ( new Y.Do.AlterReturn(msg, "altered return") );
}
var t = new TestObj("testobj");
var s = new TestScope("testscope");
// will it work with prototypes?
// Y.Do.before(TestObj.prototype, "printName", bef);
Y.log("\n\nNo error:\n");
Y.Do.before(bef, t, "printName", s, 1, 2, 3);
//Y.before(bef, t, "printName", s);
Y.after(aft, t, "printName", s);
Y.log("-- " + t.printName("testing", "secondarg"));
Y.log("\n\nAlter Args:\n");
Y.before(alterArgs, t, "printName", s);
Y.log("-- " + t.printName("testing"));
Y.log("\n\nAlter Return:\n");
Y.after(alterReturn, t, "printName", s, 1, 2, 3);
Y.log("-- " + t.printName("testing"));
Y.log("\n\nError:\n");
Y.before(causeError, t, "printName", s, 'a', 'b');
// Y.Do.before(causeError, t, "printName", s);
Y.log("-- " + t.printName("testing"));
Y.log("\n");
Y.augment(TestObj, Y.Event.Target);
// var obj = new Y.Event.Target();
var obj = new TestObj('ETTest');
obj.subscribe('foo', function() {
console.info('foo!');
});
// obj.fire('foo');
function bef2(msg, obj, a3) {
// alert('bef: ' + Y.Lang.dump(Y.Array(arguments)));
}
function bef3(msg, obj, a3) {
}
function aft2(msg, obj, a3) {
}
function aft3(msg, obj, a3) {
}
obj.before(bef2, "printName", obj, 1, 2, 3);
obj.before(bef3, "printName", obj, 1, 2, 3);
obj.after(aft2, "printName", obj, 1, 2, 3);
obj.after(aft3, "printName", obj, 1, 2, 3);
/* Output:
No error:
before testing, scope: testscope
testobj 'printName' testing
after testing, scope: testscope
-- true
Alter Args:
before testing, scope: testscope
altered args!
testobj 'printName' newarg
after newarg, scope: testscope
-- true
Alter Return:
before testing, scope: testscope
altered args!
testobj 'printName' newarg
after newarg, scope: testscope
new return val!
-- altered return
Error:
before testing, scope: testscope
altered args!
ack!
-- false
*/
})();
</script>
<body>
<input id="addButton" type="button" value="Add link" />
</body>
</html>