#151 √ not_for_core
Webbles

Extending YUI objects with prototype

Reported by Webbles | June 6th, 2008 @ 06:13 PM

We use both YUI and prototype and have classes built using both libraries. When I take YUI class and subclass it with a prototype class, I get a javascript error saying that parent.subclasses is undefined. That is because YUI doesn't add the subclasses array to its objects while prototype does and expects it to be there.

I've modified the prototype source code to have a check for subclasses and if not defined, define it to an empty array. It would be nice if this was in the main source code so I wouldn't have to keep modifying prototype on each release.

Comments and changes to this ticket

  • Juriy Zaytsev

    Juriy Zaytsev June 6th, 2008 @ 06:40 PM

    And what is stopping you from creating parent classes via Prototype?

  • John-David Dalton

    John-David Dalton June 7th, 2008 @ 03:36 AM

    Could you please attach a patch to this ticket :)

    I don't know about this bug. I would like to hear what the core devs think.

    I can see this being "not for core".

  • Webbles

    Webbles June 7th, 2008 @ 05:47 AM

    Here's a diff.

    As for Juriy's question, nothing is stopping us. We have parent classes that are prototype based as well as parent classes that are YUI based. But when we try and extend a YUI class using the Prototype Class.create() method, we have to have this change. This is because YUI doesn't add the subclasses array to the objects and Prototype expects the parent object to have it.

    I'm not sure how I'd go about fixing this without modifying the code. I guess I could wrap the Class.create() function and do the check for the subclasses array in the wrapping function. I haven't tried that so I don't know if it will work.

  • Juriy Zaytsev

    Juriy Zaytsev June 7th, 2008 @ 06:13 AM

    Hmm, could you try if this works?

    Class.create = Class.create.wrap(function(proceed, parent) {
      if (Object.isFunction(parent))
        parent.subclasses = parent.subclasses || [];
      return proceed.apply(Class, $A(arguments).slice(1));
    })
    
  • Webbles

    Webbles June 9th, 2008 @ 07:05 PM

    That code worked great.

  • Andrew Dupont

    Andrew Dupont June 9th, 2008 @ 09:42 PM

    • → State changed from “new” to “not_for_core”

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

The Prototype JavaScript library.

Shared Ticket Bins

People watching this ticket

Attachments