0

I'd like to extend a jQuery plugin.

In this paste of the plugin I want to extend, I've hidden everything else apart from the main structure, what I believe is relevant to what I want to do (I'm probably wrong) and one particular method I want to extend:

(function (factory) {
    if (typeof exports === 'object') {
        // CommonJS
        module.exports = factory(require('jquery'));
    } else if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['jquery'], factory);
    } else {
        // Browser globals
        factory(jQuery);
    }
}(function ($) {

    /**
     * Kontrol library
     */
    "use strict";

    /**
     * Definition of globals and core
     */
    var k = {}, // kontrol

    // ...

    k.c = {};
    k.c.d = $(document);
    k.c.t = function (e) {
        return e.originalEvent.touches.length - 1;
    };

    /**
     * Kontrol Object
     *
     * Definition of an abstract UI control
     *
     * Each concrete component must call this one.
     * <code>
     * k.o.call(this);
     * </code>
     */
    k.o = function () {

        // ...

    };


    /**
     * k.Dial
     */
    k.Dial = function () {

        // ...

        // this is one of the methods I want to extend:

        this.change = function (v) {
            this.cv = v;
            this.$.val(this.o.format(v));
        };

        // ...

    };

    $.fn.dial = $.fn.knob = function (o) {
        return this.each(
            function () {
                var d = new k.Dial();
                d.o = o;
                d.$ = $(this);
                d.run();
            }
        ).parent();
    };

}));

Ideally I would like to better understand what is going on there in terms of architecture but I'd be happy if anyone could point me out any direction on how to extend the change() method.

I've seen answers like this one, but what would be the Namespace and pluginName in my case?

Community
  • 1
  • 1
zok
  • 6,065
  • 10
  • 43
  • 65
  • So the actual question is how to add behaviour to an existing function? – Randy Sep 09 '16 at 11:30
  • @Randy, yes I think it could be put like that...but I might want to remove code from the original function too, would that need a different approach? – zok Sep 09 '16 at 11:32
  • I'm confused. What is the problem? Do you want to avoid modifications to the original library and create your own methods, or what's stopping you from pasting your code right there and try to find out if it's working? – Randy Sep 09 '16 at 11:36
  • @Randy, I know I could just paste code right there but I'd like to avoid that, if possible. Honestly, that's mostly for my own learning, I don't think there will be a problem if I just modify the plugin (in this case). – zok Sep 09 '16 at 11:39

0 Answers0