How to wrap the jQuery function

Rick Hanlon II

Question

I'd like to know the best way I can wrap the jQuery function while retaining all functionality. Essentially I want to call $('#someId') but have it operate as $('#' + id + 'someId') by wrapping the function, modifying the arguments, and passing it through to the original jQuery function.

Motivation

I have a section of JS that will reuse the same variable winId which is concatenated and passed to jQuery. Instead of writing

$('#' + winId + 'someId').html();
$('#' + winId + 'someOtherId').css();
...
$('#' + winId + 'someThirdId').text();

throughout the whole file, I want to wrap the jQuery function so I can just call

$('#someId').html();
$('#someOtherId').css();
...
$('#someThirdId').text();

and and have winId added in before passing through to $.


My attempt

Here's what I'm thinking as a wrapper:

(function() {
    var fn = $;

    return $ = function() {
       for ( var i = 0; i < arguments.length; i++ ) {
          if ( typeof arguments[i] == 'string') {
             arguments[i] = /* code to add in winId, omitted */
          }
       }
       return fn.apply( this, arguments );
    }
})();

This works great, except that obviously none of the methods like $.ajax are available:

Uncaught TypeError: Object function () {
    for ( var i = 0; i < arguments.length; i++ ) {
       if ( typeof arguments[i] == 'string' ) {
          arguments[i] = /* code to add in winId, omitted */
       }
    }
    return fn.apply( this, arguments );
} has no method 'ajax' 

Note: I know I could copy the object over using jQuery.extend($, jQuery), but I'm interested in a more elegant solution than that if possible.

plalx

Here's a different implementation:

DEMO

(jQuery.fn.init = (function (init) {

    return function (selector) {
        if (typeof selector === 'string' && selector[0] === '#') {
            arguments[0] = selector.replace('#', '#prefix_');
        }

        return init.apply(this, arguments);
    };

})(jQuery.fn.init)).prototype = jQuery.fn;


$(function () {
    console.log($('#test').length);
    console.log($.ajax);
});

EDIT: Followup question: How can I apply this only within a closure? For example, within an object.

Perhaps with functions that allows to add named decorators and remove them, something like:

HTML

<div id="prefix_test"></div>

JS

var decJQ = (function (decJQ, $) {
    var decorators = {},
        init = $.fn.init;

    ($.fn.init = function () {

        for (var k in decorators) {
            if (decorators.hasOwnProperty(k)) {
                arguments = decorators[k].apply(this, arguments);
            }
        }

        return init.apply(this, arguments);
    }).prototype = $.fn;

    return $.extend(decJQ, {
        decorate: function (name, fn) {
            decorators[name] = fn;
        },
        undecorate: function (name) {
            delete decorators[name];
        }
    });

})(window.decJQ || {}, jQuery);

decJQ.decorate('idPrefix', function (selector) {
    if (typeof selector === 'string' && selector[0] === '#') {
        arguments[0] = selector.replace('#', '#prefix_');
    }

    return arguments;
});

$(function () {
    console.log($('#test').length); //1

    decJQ.undecorate('idPrefix');

    console.log($('#test').length); //0
});

EDIT 2: You could also go for something extremely simple, such as:

(function ($) {
    //use $ which has been wrapped
})(function () {
    //do some manipulations
    return jQuery.apply(this, arguments);
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Wrap jQuery plugin with a function

From Dev

How important it is to wrap jquery code in $(document).ready() function?

From Dev

How to wrap function in haskell?

From Dev

How to wrap function in haskell?

From Dev

Is there a way to wrap a jquery function to an object?

From Dev

Wrap function Jquery for multiple elements

From Dev

Wrap Angular App in a jQuery function

From Dev

How to wrap printf() into a function or macro?

From Dev

Magic sprintf function - how to wrap it?

From Dev

How to wrap an asynchronous function in a promise?

From Dev

How to wrap an element with a div in jQuery?

From Dev

jQuery: how to wrap selected text

From Dev

Using Jquery wrap function with absolute placed element

From Dev

How to reorder jquery UI tabs using $.fn.tabs and underscorejs _.wrap function

From Dev

How to wrap a function in a function to avoid code repetition

From Dev

How to use jQuery wrap in jQuery variables?

From Dev

How to write a bash function to wrap another command?

From Dev

How to wrap "descending sort in data table" into a function?

From Dev

How to wrap function that takes unlimited number of arguments

From Dev

In RVCT Compile Environment, how to wrap a function

From Dev

How to wrap RHS terms of a formula with a function

From Dev

How to wrap ncurses's printw function?

From Dev

How to use SWIG to wrap std::function objects?

From Dev

How to use original parameter in `_.wrap` function in Underscore

From Dev

How to work with stl with functor wrap of function pointer?

From Dev

In RVCT Compile Environment, how to wrap a function

From Dev

How to wrap JSON to a javascript function call

From Dev

How to wrap groups of two elements in jQuery?

From Dev

How to wrap a jQuery control as an angular directive?

Related Related

  1. 1

    Wrap jQuery plugin with a function

  2. 2

    How important it is to wrap jquery code in $(document).ready() function?

  3. 3

    How to wrap function in haskell?

  4. 4

    How to wrap function in haskell?

  5. 5

    Is there a way to wrap a jquery function to an object?

  6. 6

    Wrap function Jquery for multiple elements

  7. 7

    Wrap Angular App in a jQuery function

  8. 8

    How to wrap printf() into a function or macro?

  9. 9

    Magic sprintf function - how to wrap it?

  10. 10

    How to wrap an asynchronous function in a promise?

  11. 11

    How to wrap an element with a div in jQuery?

  12. 12

    jQuery: how to wrap selected text

  13. 13

    Using Jquery wrap function with absolute placed element

  14. 14

    How to reorder jquery UI tabs using $.fn.tabs and underscorejs _.wrap function

  15. 15

    How to wrap a function in a function to avoid code repetition

  16. 16

    How to use jQuery wrap in jQuery variables?

  17. 17

    How to write a bash function to wrap another command?

  18. 18

    How to wrap "descending sort in data table" into a function?

  19. 19

    How to wrap function that takes unlimited number of arguments

  20. 20

    In RVCT Compile Environment, how to wrap a function

  21. 21

    How to wrap RHS terms of a formula with a function

  22. 22

    How to wrap ncurses's printw function?

  23. 23

    How to use SWIG to wrap std::function objects?

  24. 24

    How to use original parameter in `_.wrap` function in Underscore

  25. 25

    How to work with stl with functor wrap of function pointer?

  26. 26

    In RVCT Compile Environment, how to wrap a function

  27. 27

    How to wrap JSON to a javascript function call

  28. 28

    How to wrap groups of two elements in jQuery?

  29. 29

    How to wrap a jQuery control as an angular directive?

HotTag

Archive