Observable pattern

23 views
Skip to first unread message

Nickolay Platonov

unread,
Jan 13, 2011, 12:26:47 PM1/13/11
to joose-js
Hello,

I finally wrote the documentation for the JooseX.Observable: http://samuraijack.github.com/JooseX-Observable/doc/html/JooseX/Observable.html

Its a Role, which implements the Observable pattern. In the simplest case it acts other similar systems, additionally
events could be treated as hierarchical and bubble up along the whole/part chain.

Regards, Nickolay

Tom

unread,
Jan 14, 2011, 7:00:50 AM1/14/11
to joos...@googlegroups.com
Hi Nickolay,
 
That's great.
 
Thanks again.
 
Best regards,
Tom

2011/1/13 Nickolay Platonov <nick...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Joose" group.
To post to this group, send email to joos...@googlegroups.com.
To unsubscribe from this group, send email to joose-js+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joose-js?hl=en.

Alexander Fell

unread,
Feb 28, 2013, 10:27:27 AM2/28/13
to joos...@googlegroups.com
Hi,

I have a question, related to this. I want to create a role "Bindable" that will automatically map "trigger" to dispatch an event on that property. Unfortunately, I'm having trouble looking at the source and finding the correct phase to modify the attributes.

What I want to do is to loop over all the attributes and set the trigger value to a callback that dispatches an event based on the attribute name. This is all very easy, however I cannot find the correct place to do this before the stem is closed or the flatten system has processed.

So so far, stuck:

Role("Bindable", {
        does: JooseX.Observable
});

Any help appreciated.

Nickolay Platonov

unread,
Feb 28, 2013, 1:04:59 PM2/28/13
to joos...@googlegroups.com
Hi,

Its better to not use trigger for that - what if user will provide own trigger - it will overwrite the one you create. Overall its nothing hard, I'll check how it can be done tomorrow (pretty later here right now).

--
You received this message because you are subscribed to the Google Groups "Joose" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joose-js+u...@googlegroups.com.

To post to this group, send email to joos...@googlegroups.com.



--
Nickolay

Nickolay Platonov

unread,
Mar 2, 2013, 8:11:55 AM3/2/13
to joos...@googlegroups.com
So here's a quick example (see attachment for full example and below the content of "observable.js" file from it)

Role('JooseX.Observable.Attribute', {
   
    override : {
       
        getSetter : function () {
            var original    = this.SUPER()
           
            // here "this" is the attribute instance
            var me          = this
           
            return function (value) {
                var prevValue       = me.getValueFrom(this) // or just this[ me.slot ] for simple cases
               
                // here - "this" is the instance of class that has this attribute
                var result  = original.apply(this, arguments)
               
                this.fireEvent(me.name + 'change', value, prevValue)
               
                return result
            }
        }
    }
})


Role('JooseX.Observable.Class', {
    does : [
        JooseX.Observable
    ],
    
    trait   : Role({
        has : {
            defaultAttributeClass       : Class({ does : JooseX.Observable.Attribute, isa : Joose.Managed.Attribute })
        }
    })
})


Class('My.Observable.Class', {
    does        : JooseX.Observable.Class,
     
    has     : {
        attribute       : {
            // to create getter/setter
            is  : 'rw'
        }
    }
})

var instance = new My.Observable.Class({
    attribute       : 1
})


instance.on('attributechange', function (event, newValue, oldValue) { console.log("handler works, new value = " + newValue + ", old value = " + oldValue) })

instance.setAttribute(2)

Things to note:
1) we created a special role for attribute JooseX.Observable.Attribute', so this feature can be combined with other features.
2) We created a special role JooseX.Observable.Class, that
 - does "JooseX.Observable" so any class consuming that role will also does that role
- has a "trait" - role that will be applied to the meta-class of the consuming class. This trait just changes the default attribute class to the new attribute class (that does our JooseX.Observable.Attribute)
3) Setters are not used during initial instance construction, so the event will never be emited for that.

Hope that makes sense :)

Regards, Nickolay
--
Nickolay
observable.tar.gz
Reply all
Reply to author
Forward
0 new messages