PDA

View Full Version : [INFOREQ]allow singleton as mixins



christophe.geiser
10 Mar 2011, 7:28 AM
Hi all
This is not a bug, but I am wondering if it would not make sense to allow singleton to be utilized as mixins.

This change (ext-core-debug, l. 2656):

mixin: flexSetter(function(name, cls) {
// var mixinPrototype = cls.prototype,
var mixinPrototype = cls.prototype ? cls.prototype : cls,
myPrototype = this.prototype,
i;
...would allow it.

Cheers and good luck with the release
Christophe

evant
13 Mar 2011, 11:14 PM
In what kind of scenario would you use this?

christophe.geiser
14 Mar 2011, 12:34 AM
Yep, sure.
An example from my xForms application:
I have one singleton where a couple of xml-related attributes and functionalities are defined (xml namespace definition, parsing, serializing, xslt transformation, ...). Those methods and properties are shared accross different xForms-aware objects (like xforms instances, models and fields) and hence injected as mixins.

I could use the @createdFn from Ext.ClassManager.create to add something like :
function() { myNewSingletonFromClass = new this() ... } if the change I am proposing does not fit the code base, but I think it would be clearer to take advantage of the Ext.define ... singleton = true ... scenario.

Cheers
Christophe

evant
14 Mar 2011, 3:30 AM
Right, but since it's a singleton, why not just call the methods directly? What benefit do you get from mixing them in to the current class?

christophe.geiser
14 Mar 2011, 6:36 AM
Simply because of "this" ; )

You might want to be able to inject default methods into Ext components via mixins and keep specific methods (with the same name) for more specialized components (that would not be overriden by the mixin).

A concrete example from my app:
bind: a method allowing to bind Ext components with xml elements (change of the xml element value or state (readonly, visiblity, validation) is reflected in any binded component and vice-versa). The default bind method is defined in a singleton and mixed into all xforms aware components. However more specific bind mechanisms need to be implemented for some extra components (e.g. binding an HtmlEditor will need some special care compared to a simple textField).

At the same time, I also need to invoke the same methods from elsewere in the app (hence the singleton).

I thought Mixins were introduced to handle such cases.
Cheers
C.