Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 666
Fresheyeball Avatar asked Nov 03 '12 17:11

Fresheyeball


2 Answers

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);
};
like image 88
gondo Avatar answered Oct 28 '22 12:10

gondo


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/

like image 4
amustill Avatar answered Oct 28 '22 13:10

amustill