Search

4/24/2008

YAHOO.lang.augmentProto

YAHOO.lang.augmentProto


YAHOO.namespace('example');

// Create a class Foo for some greater purpose
YAHOO.example.Foo = function () {
/* code specific to Foo */

// Add a custom event for the instance
this.createEvent('interestingMoment');
}
YAHOO.example.Foo.prototype.doSomething = function() {
/* ..do something interesting... */

// Fire off the custom event
this.fireEvent('interestingMoment');
}

// Make the custom event stuff possible via augmentProto
YAHOO.lang.augmentProto(YAHOO.example.Foo, YAHOO.util.EventProvider);

var f = new YAHOO.example.Foo();

// Add some event listeners
f.subscribe('interestingMoment', function () {
var p = YAHOO.util.Dom.get('demo_p1');
p.innerHTML = 'I was notified of an interesting moment';
});
f.subscribe('interestingMoment', function () {
var p = YAHOO.util.Dom.get('demo_p2');
p.innerHTML = 'I was also notified of an interesting moment';
});

// Add a listener to the button to call the instance's doSomething method
YAHOO.util.Event.on('demo','click', function () { f.doSomething() });

沒有留言: