Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: object[method]() not working or how to build a jQuery plugin for a plain Javascript code

I need help making a jQuery plugin for one of my vanilla JS scripts, this here is the current jQuery plugin, but the next version works with more methods and I need to address them all somehow.

Currently I was working on this

(function($) {
    var t;  
    $.fn.KUTE = function( method, start, end, ops ) { // method can be Animate(), fromTo(), to(), stop(), start(), chain(), pause(), stop(), etc
        return this.each(function(){        
            if ( method === 'to' ) {                
                t = new KUTE[method]( this, null, end, ops );
            } else if ( method === 'fromTo' || method === 'Animate' ) {
                t = new KUTE[method]( this, start, end, ops );
            } 
            if ( t !== undefined && typeof t[method] === 'function' ) {
                console.log(t) // this shows proper object
                t[method]() // this doesn't work
            }   
        });
    };  
})(jQuery);     

Why t[method]() doesn't work and how can I make it work?

UPDATE: I am adding here some sample code on how things work around this code. Basically I build a tween object

var tween = $(div).KUTE('to', { left: tl }, { easing: easing, duration: 1000 } );

Then I need to start() it, stop() it and other methods.

$(tween).KUTE('start'); // this should basically be it.

Now, I've been reading about some Javascript stuff like call() and apply() and I was kinda thinking this may be required in order to work, but even so the t[method].call(t) // where t is "this", doesn't work. I hope I've put it right to indicate my problem, please correct me if anything wrong.

Thanks so much.

like image 236
thednp Avatar asked Jul 30 '15 17:07

thednp


People also ask

What is the problem with JavaScript?

These days, most cross-browser JavaScript problems are seen: When poor-quality browser-sniffing code, feature-detection code, and vendor prefix usage block browsers from running code they could otherwise use just fine. When developers make use of new/nascent JavaScript features, modern Web APIs, etc.)

How a plugin is used in jQuery?

A jQuery plugin is simply a new method that we use to extend jQuery's prototype object. By extending the prototype object you enable all jQuery objects to inherit any methods that you add. As established, whenever you call jQuery() you're creating a new jQuery object, with all of jQuery's methods inherited.

What is $() JavaScript?

The $() function The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is: document.

How are plugins implemented in jQuery?

These plugins are implemented. Approach: In Jquery Plug-in is a code that is needed in a standard javascript file. Plugins help in providing different methods that can be used with different jquery library methods. To Obtain a perfect or compatible code use this.each which is is used to perform over a set of matched elements.

How to make jQuery not use the $with jQuery?

The $ variable is very popular among JavaScript libraries, and if you're using another library with jQuery, you will have to make jQuery not use the $ with jQuery.noConflict (). However, this will break our plugin since it is written with the assumption that $ is an alias to the jQuery function.

What is $this object in jQuery?

This object contains all of the methods you've been using ( .css (), .click (), etc.) and all of the elements that fit your selector. The jQuery object gets these methods from the $.fn object.

Why do jQuery plugins return a value instead of a method?

@yuval -- typically jQuery plugins return jQuery or a value, not the plugin itself. That's why the name of the method is passed as an argument to the plugin when you want to invoke the plugin. You could pass any number of arguments, but you'll have to adjust the functions and the argument parsing.


1 Answers

I think I found out why, my last comment above explains it and I made a different jQuery function.

(function($) {
    $.fn.KUTE = function( method, start, end, ops ) { // method can be Animate(), fromTo(), to(), stop(), start(), chain(), pause() 
        var tws = [], i, l = this.length;

        for (i=0;i<l;i++){
            var mt = this[i][method];
            if ( typeof mt === 'function' ) {
                mt.apply(this[i]);
            }    
            if ( method === 'to' ) {
                tws.push( new KUTE[method]( this[i], start, end ) ); // here start is end and end is ops 
            } else if ( method === 'fromTo' || method === 'Animate' ) {
                tws.push( new KUTE[method]( this[i], start, end, ops ) ); 
            }           
        }
        return tws;
    };
})(jQuery); 

Next to do is, that if this.length > 1 do a nice little FOR() instead of the super bad and ugly jQuery.each().

Issue solved, see my codepen for an example.

UPDATE: I also added the FOR and it works as expected. I updated the answer above.

like image 104
thednp Avatar answered Oct 03 '22 23:10

thednp