[ 2 posts ]

caseymct

  • Joined: Fri Jan 28, 2011 9:31 pm
  • Posts: 15
  • Offline
  • Profile
Tags:

How to extend an already customized widget

Post Posted: Thu Sep 01, 2011 8:51 am
+0-
I have a widget I've already written (called Picker), that extends Widget.

Y.extend(Picker, Y.Widget, {
...
}

what I'd like is create another widget, DonorPicker, that extends Picker and has all the same attributes and functionality as Picker; I just want to add an additional attribute and additional function call. What is the best way to do this?

So say Picker has defined
doSomething : function () {},
doSomething2 : function () {}

From DonorPicker, I'd like to have doSomething already defined, and just be able to override doSomething2 to do something slightly different.

looked at the docs and found a few different examples and I'm confused, any help appreciated. thanks!!

Juan Ignacio Dopazo

YUI Contributor

  • Username: jdopazo
  • Joined: Fri Oct 02, 2009 5:39 am
  • Posts: 619
  • Location: Buenos Aires, Argentina
  • Twitter: juandopazo
  • GitHub: juandopazo
  • Gists: juandopazo
  • Offline
  • Profile
Tags:

Re: How to extend an already customized widget

Post Posted: Thu Sep 01, 2011 9:30 am
+0-
Hey! As is true of many things in JavaScript, there are many ways of doing it. The simplest case would be to continue using inheritance. Just create a new "class" that extends your Picker class:
Code:
function DonorPicker() {
  DonorPicker.superclass.constructor.apply(this, arguments);
}
Y.extend(DonorPicker, Picker, {
  doSomething2: function () {
    // overwrite the Picker functionality or... call the superclass!
    DonorPicker.superclass.doSomething2.apply(this, arguments);
  }
});

Another way would be to use Extensions to keep your inheritance chain short but still reuse code.
  [ 2 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