0

OO プリンシパルに基づいて新しい jquery プラグインを作成しました。プラグインは、それぞれ独自の設定/オプションを持つ複数のインスタンスを持つことができる必要があります。ただし、私が今実装した方法では、最初の設定がすべてのインスタンスに使用されます。

私はこれに対する解決策を見つけることができないようです:

私はそれを開きます:

// the semi-colon before the function invocation is a safety 
// net against concatenated scripts and/or other plugins 
// that are not closed properly
; (function ($, window, document, undefined) {

私は次の機能を実行します。

    /**
    * Method for formatting a certain amount inside a provided element 
    * (the element can be an input or a regular span).
    *
    * @param object element         : Where the value will come from.
    * @param array options              : The different options to format     the amount. The options are culture, valuta and the amount of rounding.
    *
    * @return void;
    */
var AmountFormatter = function (element, options) {
    var elem = $(element);
    var self = this;

    //Defaults in case a value is missing
    var defaults = {
        culture: "",
        currency: "",
        rounding: 2
    };  

    // jQuery has an extend method that merges the 
    // contents of two or more objects, storing the 
    // result in the first object. The first object 
    // is generally empty because we don't want to alter 
    // the default options for future instances of the plugin
    var settings = $.extend({}, defaults, options || {});
    self._settings = settings;

    //Various functions

            //Start using the amountFormatter, this is a public function, so if you want to use the logic without a jquery selector, you can :).
    this.init = function (elements) {

        //If you the plugin is accessed with a jquery selector, format it as followed:
        if (elements instanceof jQuery) {
            //Check the value of each element and update it accordingly
            elements.each(function () {
                var $this = $(this);
                var elementIsInput = $this.is("input");

                value = $this.val() == "" ? $this.text().trim() : $this.val().trim();
                if(typeof value === 'undefined' || value === "") {
                    return ""; 
                }
                value = thousandSeperator(convertNumber(roundingOfNumber(value, self._settings.rounding)));

                //Checks whether we need to add the new amount as text or as a value
                return elementIsInput === true ?
                    elem.val(addCurrencyToNumber(value)) :
                    elem.text(addCurrencyToNumber(value));
            });
        }
        //Otherwise we:
        else {
            //First of, check if the provided variable is at least set and has at least one element
            if (elements != undefined || elements != null || elements.length !== 0) {
                if (elements instanceof Array) {
                    for (var i = 0; i < elements.length; ++i) {
                        var value = elements[i].toString();
                        elements[i] = addCurrencyToNumber(thousandSeperator(convertNumber(roundingOfNumber(value, self._settings.rounding))));
                    }
                    return elements;
                }
                else {
                    var value = elements.toString();
                    return addCurrencyToNumber(thousandSeperator(convertNumber(roundingOfNumber(value, self._settings.rounding))));
                }
            }
        }
    };


    this.init(elem);
};

ここでは、amountFormatter を初期化します。

/*
* Initialize the amountFormatter
*/
$.fn.amountFormatter = function (options) {
    return this.each(function () {          
        var $this = $(this);            

        if ($this.data('amountFormatter')) return;

        var amountFormatter = new AmountFormatter($this, options);
        $this.data('amountFormatter', amountFormatter);
    });
};

そして、私はそれを閉じます:

})(jQuery, window, document);

私のページでの使用方法:

最初のインスタンス:

        // With options...
        container.amountFormatter({
            culture: "NL",                   
            currency:  "€",
            rounding: decimals
        });

        //Initiate the plugin and add the array to the list...
        var amountFormatterData = container.data('amountFormatter');

2 番目のインスタンス:

// With options...
        container.amountFormatter({
            culture: "NL",                   
            currency:  " ",
            rounding: decimals
        });

//Initiate the plugin and add the array to the list...
        var amountFormatterData = container.data('amountFormatter');

そのため、2 番目のインスタンスは最初のインスタンスの値を取得し、各インスタンスが独自の設定を使用するようにはできません。

4

1 に答える 1