On 2 avr, 22:41, "Ryan Gahl" <[EMAIL PROTECTED]> wrote: > Javascript is an expressive language. You can achieve what you are asking > without baking it into the method like that, you just have to stop trying so > hard to do things in one function call (it's ok to have 2 function calls to > achieve something that is a composite of two simpler steps). And by leaving > our illustrious lower-level prototype functions as they are, it supports > comfy use for those cases where the extra functionality is not needed (i.e. > where only that first function call is required). Once you use prototype for > about a year you will learn to like the fact the the core functions are left > simple. So while I'd love to see my version of Object.inherit included in > the core, I would not want Object.extend to change, nor would I want > Class.create() to change. >
Yeah, I know what you mean. Thanks for the tip though. > As for the Observable/Observer thing, did you check out my other blog post > on EventPublisher? If you need that functionality, you may that interesting > as > well:http://www.someelement.com/2007/03/eventpublisher-custom-events-la-pu... > Well, it's not quite what I had in mind. I have already seen something similar in "Ajax in action", and I already implemented an EventQueue system (which works fine, using async calls with Rico support), but I wanted to create objects that could interact with each other directly withouth having to implement system hooks every time. Here's what I wrote (in about 15 minutes) : // -------------- begin ---------------------------- /** * Observable * * This object encapsulate the Obserable part of the Observer pattern. * * @see Object.inherit() for more info on how to inherit this object */ Observable = Class.create(); Observable.prototype = { /** * Constructor : build a new instance */ initialize: function() { this.observers = []; this.hasChanged = false; this._timer = null; }, /** * Add a new observer to the observers list * * @see Observer * * @param observer an object that implements the Observer */ addObserver: function(observer) { this.observers.push(observer); }, /** * Notify all observers if the Observable has changed. If a delay is * provided, the observers will only be notified when * * @see Observable.setChanged() * * @param message a message to send to the observers * @param delay (optional) a delay in millis (0 or null = notify now) */ notifyObservers: function( message, asyncDelay ) { if ( this.hasChanged ) { var _notifyFn = function() { for (var i=0, len=this.observers.length; i<len; i++) { this.observers[i].update(this, message); } this.hasChanged = false; }.bind(this); // if notifications are waiting for a delay, clear this delay now if ( this._timer ) { clearTimeout(this._timer); } if ( asyncDelay && asyncDelay > 0 ) { this._timer = setTimeout( _notifyFn, asyncDelay ); } else { _notifyFn(); } } }, /** * Remove an observer */ removeObserver: function(observer) { this.observers = this.observers.without(observer); }, /** * Set the Observable as "modified". Any subsequent call to * notifyObservers() will notify them. */ setChanged: function() { this.hasChanged = true; } }; /** * Observer * * An Observer implementation of the Observer pattern. Objects that inherits * this object must override the methode update(observable, message) * * @see Object.inherit() to see how to inherit from this object */ Observer = Class.create(); Observer.prototype = { initialize: function() {}, /** * ** Must be implemented ** * * This method is called by the Observable object to warn this observer about * changes. * * @param observable the caller object (Observable) * @param message any message from the Observable */ update: function(observable, message) { throw "NotImplmentedException: Observer.update"; } }; // test code // observer test var TestObserver = Class.create(); Object.inherit(TestObserver, Observer); Object.extend(TestObserver.prototype, { initialize: function() { this.base(); }, update: function(observable, message) { alert( observable + " = " + message ); } } ); var TestObservable = Class.create(); Object.inherit(TestObservable, Observable); Object.extend(TestObservable.prototype, { doTest: function(message) { this.setChanged(); this.notifyObservers(message, 3000); } } ); var observable = new TestObservable(); observable.addObserver( new TestObserver() ); observable.doTest( "test 123" ); // alert "[object] = test 123", 3 seconds later // ------------- end ------------------------- I won't say that it is well optimized, but this simple test is quite positive. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
