[ 4 posts ]

mingfai

  • Joined: Mon Dec 15, 2008 6:58 pm
  • Posts: 22
  • Offline
  • Profile

using YUI3 Y.mix to implement Delegate Pattern and wrap YUI2

Post Posted: Wed Feb 25, 2009 10:51 pm
+0-
hi,

one of the possible use case is to make wrapper YUI3 object that extends base or widget to wrap YUI2 object. Instead of directly use YUI2 class, I create a wrapper and store the YUI2 object as a private property. say, yui3datatable._delegate = new YAHOO.widget.DataTable(...)

Ideally, Y.mix could be used to copy all properties from the delegate to the outer class, while each function is binded like Y.bind( yui2database_function, yui3datatable ) . For sure this require Y.mix to have extra argument to for binding, or expose a function for us to "transform" each property when it is being mixed.

if the Y.mix could be done to implement delegate, then the other tasks are:
  • attributes - if the YUI2 class has only one or two attributes/constructor arguments, i'll make it as a YUI3 attribute; otherwise the whole arguments to the constructor is stored. i've tried to do this before in an experiment
  • event - this would require the parent to subscribe to the event of the delegate and fire it.

then a pseudo YUI3-to-YUI2-delegate-wrapper is more or less done. does it make sense?

mingfai

  • Joined: Mon Dec 15, 2008 6:58 pm
  • Posts: 22
  • Offline
  • Profile
Tags:

Re: using YUI3 Y.mix to implement Delegate Pattern and wrap YUI2

Post Posted: Wed Feb 25, 2009 10:54 pm
+0-
forgotten to mention, the render() will be handled separately. the render code will be splitted to renderUI, bindUI, and syncUI in case by case basis. some transformation will be done, e.g. an id with # will be converted back to the YUI2 style.

the whole objective is to YUI2 features with YUI3 in a more YUI3-ish style.

Eric Ferraiuolo

YUI Developer

  • Username: ericf
  • Joined: Mon Jan 12, 2009 8:26 pm
  • Posts: 380
  • Location: Boston, MA
  • Twitter: ericf
  • GitHub: ericf
  • Gists: ericf
  • IRC: eric_f
  • YUI Developer
  • Offline
  • Profile

Re: using YUI3 Y.mix to implement Delegate Pattern and wrap YUI2

Post Posted: Thu Feb 26, 2009 11:49 am
+-1-
Seems like it could lead to complicated debugging. It would be interesting though to see this approach on a yui2 widget that's moderate in [API] size, say the TabView widget.

What types of benefits would you be looking for that make doing the wrapping worth it?
Eric Ferraiuolo

mingfai

  • Joined: Mon Dec 15, 2008 6:58 pm
  • Posts: 22
  • Offline
  • Profile

Re: using YUI3 Y.mix to implement Delegate Pattern and wrap YUI2

Post Posted: Thu Feb 26, 2009 7:29 pm
+0-
it's just an experimental idea and i dare not to say it is good. There are two issues:
1. Y.mix to support binding
2. should we/how do we wrap YUI2 with YUI3

re. 1, i just got a new usage that is to do some kind of pseudo AOP to do sth before and after any function. e.g. say the Y.mix is enhanced with an additional argument that take a function to process the function before it is assigned to the receiver, e.g.

function(prop, r, s){
if (Y.Lang.isFunction(prop) ){
return Y.bind( prop, r._delegate)
}
return prop;
}

one usage is to add logging before the fn is called, e.g.
function(prop, r, s){
if (Y.Lang.isFunction(prop) ){
return function() {
Y.log( "target: " + r._delegate + ", function: " + prop + ", arguments: " + arguments, "debug", "Y.mix injected logger")
Y.bind( prop, r._delegate)
}
}
return prop;
}

re. 2, the reasons/benefits to do in this way:
  • i want to minimize and isolate any YUI2 style code, and ultimately remove them all
  • when we need to implement more complex features, such as a datatable that is both editable and support customizing columns, it is the best way to subclass the table in YUI2. i'm not sure if it is a good idea or doable to create a YUI3 class that extend a YUI2 class, but i would prefer the subclass code to use YUI3 style.
  • we could utilize the attribute and event of Base for our own function on top of YUI2 API easier
  • for debugging, unless the console log is ported for YUI2, i indeed think it's easier to log and trace issues in YUI3. if you run with a debugger, you can step on the code anyway. i suppose you mean, if there are some kind of YUI3 attribute to YUI2 mapping, then it introduces a layer of complexity. it's true. I use Y.log to log anything with Y.JSON.stringify to log anything.

i dare not to say its a good approach. just to evolve an idea. ;)
  [ 4 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum