Opened 4 years ago

Closed 3 years ago

#15279 closed feature (wontfix)

Support for private fields and methods within widgets

Reported by: Jake Meiergerd Owned by:
Priority: minor Milestone: none
Component: ui.widget Version: git (not yet released)
Keywords: Cc:
Blocked by: Blocking:

Description (last modified by Jake Meiergerd)

I've been working lately on a framework of common jQuery widgets, built through the Widget Factory, for use in a larger application, and I've got a lot of widget inheritance going on. I've found myself wishing for a way to have a particular widget define private members that can't accidentally be overridden by another widget, because that implementer didn't realize he defined a field or method with the same name as an existing one on the base widget. In other words, I need a solution for namespace conflicts.

I've got one that works by adding some additional new functions to $.Widget.prototype, and using a custom wrapper functions for $.widget(), but it's rather cumbersome to use, as it requires widgets to call these additional functions at the beginning of almost every regular function call.

I took a shot at integrating the "tacked-on" code into the widget component itself, and it's a lot nicer to use. I'm ready to submit a pull request with the code, if there's interest.

Conceptually, the solution is as follows:

First, each widget prototype can define a field named "_privates", in the same fashion as the "options" field. When prototypes are merged, however, any "_privates" fields are not simply merged together, but are re-organized into an object structure of the following form:

_privates: {
    Namespace1: {
        Widget1: {},
        Widget2: {}
    Namespace2: {
        Widget1: {},
        Widget2: {}

Second, the proxy methods that define "_super" and "_superApply" for every method call on the widget instance now also define a "_myPrivates" field, which extracts the appropriate sub-object from the "_privates" object, based on the namespace and name of the prototype that is being proxied.

Finally, when a new instance of the widget is created, the "_privates" object on the merged prototype is cloned anew for the new instance, similar to the "options" object.

I also added the step of searching for any functions in the cloned "_privates" object and binding them to the widget instance, so they can access other fields and methods of the widget. Requiring the widget to do this manually seems like pointless boilerplate, but I'm open to discussion.

Ultimately, you get a system that looks like this:

Change History (3)

comment:1 Changed 4 years ago by Jake Meiergerd

Description: modified (diff)

comment:2 Changed 4 years ago by Jake Meiergerd

Description: modified (diff)

comment:3 Changed 3 years ago by Scott González

Resolution: wontfix
Status: newclosed

Use the language as intended.

Note: See TracTickets for help on using tickets.