jQuery pluggin -> Zepto; $.fn.extend is undefined
Asked Answered
S

2

5

I am new to zepto, and am using it as a jQuery replacement for the mobile part of a website.

So zepto doesn't have $.fn.extend. Fine that's cool with me, but I need my pluggin to work regardless of jquery or zepto.

What is zepto's alterative to fn.extend? How would you go about making a cross library extension? I've yet to find any documentation on this.

 $.fn.extend({
     lineRedNAddClass : function(option){
         $(this).css('border','red 1px solid').addClass(option);   
     }
 });

can this be made to work with both from the same script?

Scenic answered 3/11, 2012 at 17:28 Comment(0)
P
4

Zepto's extend function can be accessed via $.extend(), which is also available in the jQuery API, so we can simply extend $.fn using that.

Example:

$.extend($.fn, {
    myFunc: function() {
        $(this).css({
            color: 'red'
        });
    }
});

And here's a demo. I've loaded both libraries in to the assets, so just switch the value of $ using the top two lines. There's a consle.log included to prove that the correct library is loaded.

http://jsfiddle.net/WNTXY/

Portingale answered 3/11, 2012 at 18:3 Comment(1)
I just did and excepted, shweeet.Scenic
E
8

in other words, to make some jquery plugins work with zepto, i've added these 2 lines to the end of my zepto.js:

jQuery = Zepto;
$.fn.extend = function(obj) {
    $.extend($.fn, obj);
};
Ebersole answered 25/7, 2013 at 1:19 Comment(1)
simple and elegant!Mekong
P
4

Zepto's extend function can be accessed via $.extend(), which is also available in the jQuery API, so we can simply extend $.fn using that.

Example:

$.extend($.fn, {
    myFunc: function() {
        $(this).css({
            color: 'red'
        });
    }
});

And here's a demo. I've loaded both libraries in to the assets, so just switch the value of $ using the top two lines. There's a consle.log included to prove that the correct library is loaded.

http://jsfiddle.net/WNTXY/

Portingale answered 3/11, 2012 at 18:3 Comment(1)
I just did and excepted, shweeet.Scenic

© 2022 - 2024 — McMap. All rights reserved.