Ticket #243 (new enhancement)

Opened 1 year ago

Last modified 1 year ago

Accept arrays of arguments via rbind

Reported by: morten.barklund@tbwa-play.com Assigned to: somebody
Priority: normal Milestone: MochiKit 1.5
Component: component1 Version:
Severity: normal Keywords:
Cc:

Description

I would suggest that getElement would support array arguments (arrays of the same plain arguments normally accepted) and return them the same way. This could be done by changing DOM.js from:

    getElement: function (id) {
                var self = MochiKit.DOM;
                if (arguments.length == 1) {
                    return ((typeof(id) == "string") ?
                        self._document.getElementById(id) : id);
                } else {
                    return MochiKit.Base.map(self.getElement, arguments);
                }
            },

to something like:

    getElement: function (id) {
                var self = MochiKit.DOM;
                if (arguments.length == 1) {
                    if (MochiKit.BASE.isArrayLike(id))
                        return MochiKit.Base.map(self.getElement, id);
                    return ((typeof(id) == "string") ?
                        self._document.getElementById(id) : id);
                } else {
                    return MochiKit.Base.map(self.getElement, arguments);
                }
            },

(By the way, you forgot to setup your components for trac - the packages would be an obvious choice)

Thanks in advance for a great product.

Attachments

mochikit_rpartial.patch (9.0 kB) - added by morten.barklund@tbwa-play.com on 03/02/07 06:51:33.
Patch for suggested solution

Change History

03/02/07 04:00:55 changed by anonymous

Actually, this would be nice for other functions in MochiKit.DOM currently accepting an element (or an id of one) - the ability to accept (recursive) arrays of elements (or id's).

The following functions seem logical to change to this:

  • MochiKit.DOM.setNodeAttribute
  • MochiKit.DOM.getNodeAttribute
  • MochiKit.DOM.removeElementClass
  • MochiKit.DOM.addElementClass

It could probably not be used for hasElementClass - return value would be ambiguous (does it return an array or a boolean literal as a conjunction of results?). swapElementClass and toggleElementClass might also be interesting, but I'm not sure.

All functions only accepting a single node as argument could easily be called with map, so no change here is needed.

Thanks.

03/02/07 04:02:33 changed by morten.barklund@tbwa-play.com

The above comment was also by me - sorry :)

03/02/07 06:51:03 changed by anonymous

Okay, this was to funny to not implement. I've now started functions like getNodeAttribute as:

    getNodeAttribute: function (node, attr) {
        var self = MochiKit.DOM;
                var base = MochiKit.Base;
                if (base.isArrayLike(node))
                        return base.map(base.rpartial(self.getNodeAttribute, attr), node);
        if (base.isArrayLike(attr))
                        return base.map(base.partial(self.getNodeAttribute, node), attr);

In order for this to work, I had to create the rpartial. And the I had to create the rbind. And then I was copying code from bind, which seemed like a bad solution. So instead I created an internal function, _bind, as:

    _bind: function (func, self, preargs, postargs) {

That simply contains what bind contained before with the addition of remembering, updating and using postargs as well as preargs. And then bind and rbind is:

        /** @id MochiKit.Base.bind */
    bind: function (func, self/* args... */) {
        var m = MochiKit.Base;
        return m._bind(func, self, m.extend(null,arguments,2), []);
        },
        
        /** @id MochiKit.Base.rbind */
    rbind: function (func, self/* args... */) {
        var m = MochiKit.Base;
        return m._bind(func, self, [], m.extend(null,arguments,2));
        },

I've also added a bunch of testcase for rpartial and for partial and rpartial together. It works perfectly.

How do I go about suggesting a patch? I downloaded HEAD (revision 1269) and have created a patch attached here.

03/02/07 06:51:33 changed by morten.barklund@tbwa-play.com

  • attachment mochikit_rpartial.patch added.

Patch for suggested solution

03/02/07 06:56:11 changed by anonymous

  • summary changed from getElement accepting arrays of arguments to Accept arrays of arguments via rbind.

04/03/07 02:28:35 changed by discounted

  • cc set to NY.
  • keywords set to MESSAGE.

<a href='http://discounted.yeahost.com/cheap-discount-airfare.html '>cheap discount airfare</a> <a href='http://discounted.yeahost.com/student-discount-airfares.html '>student discount airfares</a> <a href='http://discounted.yeahost.com/cheap-last-minute-discount-airfare.html '>cheap last minute discount airfare</a> <a href='http://discounted.yeahost.com/travel-europe-airfare-discount.html '>travel europe airfare discount</a> <a href='http://discounted.yeahost.com/international-discount-airfare.html '>international discount airfare</a> <a href='http://discounted.yeahost.com/discount-airfares-air-travel-finder.html '>discount airfares air travel finder</a> <a href='http://discounted.yeahost.com/first-class-discount-airline-tickets-flights.html '>first class discount airline tickets flights</a> <a href='http://discounted.yeahost.com/cheap-air-flights-discount-airline-tickets-flights.html '>cheap air flights discount airline tickets flights</a> <a href='http://discounted.yeahost.com/european-discount-cruises.html '>european discount cruises</a> <a href='http://discounted.yeahost.com/oceania-discount-cruises.html '>oceania discount cruises</a> <a href='http://discounted.yeahost.com/murrays-discount-auto-parts.html '>murrays discount auto parts</a> <a href='http://discounted.yeahost.com/discount-golf-shoes.html '>discount golf shoes</a> <a href='http://discounted.yeahost.com/discount-dansko-shoes.html '>discount dansko shoes</a> <a href='http://discounted.yeahost.com/discount-merrell-shoes.html '>discount merrell shoes</a>

05/10/07 01:57:14 changed by morten.barklund@tbwa-play.com

  • cc deleted.
  • keywords deleted.

05/10/07 01:57:37 changed by morten.barklund@tbwa-play.com

  • milestone set to MochiKit 1.5.

Suggestion for mk1.5