Class construction order (Roles vs Classes)

37 views
Skip to first unread message

Jan Míšek

unread,
Jul 7, 2013, 5:38:16 AM7/7/13
to joos...@googlegroups.com
Hi Samurai

I am very happy user of Joose3. I have been writing applications in ExtJS, but not I am using Joose. I am working on UI framework based on Joose, hope I will be able to publish it soon. 

I have question about construction order of classes in Joose, I read article on your blog about internals, but still no luck to understand to order of construction. Could you please clarify for me what is construction order of classes and roles.

Imagine situation:

I have these roles:
Observable 
- observable to fire and listen to events
Collectionable 
- basic collection, 
- requires observable to work correctly
ManagedCollectionable
- managed collection which is aware of child items, o
- override some methods of collectionable

and this class:
Store - Managed collection with records
- uses all the roles, 
- overrides some methods of roles


Store = Class('store', {

  does: [Observable, Collectionable, ManagedCollectionable]

});

So my questions:
- What is the order of construction instances or meta classes
- What is the order of mixing everything together
- What is the order of calling of initialize methods when roles are used

or better how can i manage the order of construction


Thanks in advance.

Nickolay Platonov

unread,
Jul 7, 2013, 11:10:10 PM7/7/13
to joos...@googlegroups.com
Hi Jan,

Glad you like it! :)

The composition rules are different for methods and method overrides.

For _methods_ and attributes, the order of construction does not matter. If both class and role being consumed defines some method, class method will win.

Role('SomeRole', {
    methods : {
        someMethod : function () {}
    }
})

Class('SomeClass', {
    does : SomeRole,
    methods : {
        // class method wins
        someMethod : function () {}
    }
})

If class consumes 2 roles, having the same method there will be a conflict and class must provide own implementation of that method to resolve it.

So when using only methods, the order of roles in the "does" does not matter.

But it does matter when using method overrides. They don't conflict - instead they sum up with each other and with the corresponding method.
Method modifiers from roles are applied from left to right, by the role's position in "does" builder.

Role('SomeRole1', {
    override : {
        someMethod : function () {
            console.log(1)
            this.SUPERARG(arguments)
        }
    }
})

Role('SomeRole2', {
    override : {
        someMethod : function () {
            console.log(2)
            this.SUPERARG(arguments)
        }
    }
})

Class('SomeClass', {
    does : [ SomeRole2, SomeRole1 ],
    methods : {
        // class method wins
        someMethod : function () {
            console.log(3)
        }
    }
})

var a = new SomeClass()
a.someMethod()

will give us: 213

Some additional info here: http://joose.github.io/Joose/doc/html/Joose/Manual/Roles.html

So my questions:
- What is the order of calling of initialize methods when roles are used

or better how can i manage the order of construction

In your case I guess for the "initialize" method in your roles you can use method modifiers (after/before/override depending from situation).
Those modifiers will be applied in order they are listed in "does" builder of the consuming class.

Regards, Nickolay

--
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.
Visit this group at http://groups.google.com/group/joose-js.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Nickolay

Jan Míšek

unread,
Jul 10, 2013, 6:20:36 AM7/10/13
to joos...@googlegroups.com
Hi Nickolay

Thank you very much for clarification. It helped us so much.

Regards
Jan

Dne pondělí, 8. července 2013 5:10:10 UTC+2 SamuraiJack napsal(a):
Reply all
Reply to author
Forward
0 new messages