Hacker News new | ask | show | jobs
by DonHopkins 3105 days ago
I wrote a prototype of a questionnaire system using jQuery Mobile version 1.0, to see what it was like [1].

The problem I wanted to solve mapped pretty closely to exactly what jQuery Mobile was designed to do: page through a bunch of screens with controls on them. But the results were pretty "blah", and I needed a lot more control over the presentation and behavior than it provided.

Instead, I just used raw jQuery, which worked fine.

But I did use end up using one jQuery UI file "jquery.ui.widget.js" for its "$.widget" Widget Factory [2] and Widget Plugin Bridge [3], which provided some useful plumbing. But I didn't use any of jQuery UI's actual widgets. I also used $.widget for jQuery pie menus [4].

If there's a better alternative or idiom than $.widget, I'd love to know! But it worked fine for me, and I didn't need to drag in the rest of jQuery UI.

The weird thing about jQuery is: a "jQuery object", the result of querying a selector, is not an actual widget or DOM object -- it's the result of a query, which might contain any number of "objects", or none. It's ephemeral, not for storing persistent state like a widget.

So you need another way to represent and bridge messages from "jQuery objects" created by querying selectors, to actual stateful "widget" objects associated with concrete DOM objects, and that's what $.widget provides.

It creates a new "widget" object the first time you call it, and after that it updates and sends commands to the existing widget (or widgets).

Because of the way jQuery selectors work, you can actually create and configure a whole bunch of widgets at the same time with one $.widget command! Which is useful: "make each object with this class an instance of that widget with these parameters". And you can use $.widget with jQuery selectors to update parameters and broadcast messages to multiple widgets at once.

Suffice it to say, it's not how you'd design a user interface widget toolkit if you were you starting from Scratch [5]. But it is, if you were starting from jQuery.

[1] http://donhopkins.com/home/prototype/prototype.html

[2] http://api.jqueryui.com/jQuery.widget/

[3] http://api.jqueryui.com/jQuery.widget.bridge/

[4] https://github.com/SimHacker/jquery-pie/blob/master/javascri...

[5] https://wiki.scratch.mit.edu/wiki/Smalltalk