質問

I have a question of which someone might find this much simpler than I do, but alas, I don't have much experience with custom jQuery plugins.

The previous developer at my place of work left me with a lot of left-over plugins that don't seem to work very well, most which I've been able to fix but this which has been bugging me for a while.

It is a custom Multiple Suggestion plugin (called multisuggest) written in jQuery, and it has a set of functions that it uses internally (*e.g. setValue to set the value of the box, or lookup to update the search)*

It seems he's tried to call these plugin functions from an external script (this exteranl script specifically imports newly created suggestions into the multisuggest via user input and sets the value) like this:

this.$input.multisuggest('setValue', data.address.id, address);

This seems to call the function as it should, except the second and third parameters don't seem to be passed to the function (setValue receives nothing), and I don't understand how I can get it to pass these. It says it is undefined when I log it in the console. The functions are set out like this (I've only including the one I'm using and an internal function from multisuggest called select that actually works):

    MultiSuggest.prototype = $.extend(MultiSuggest, _superproto, {

    constructor : MultiSuggest, 
    select: function () { // When selecting an address from the suggestions
        var active, display, val;

        active  = this.$menu.find('.active');
        display = active.attr('data-display');
        val = active.attr('data-value');
        this.setValue(display, val, false); // This works, however when I do it as shown in the above example from an external script, it doesn't. This is because it doesn't receive the arguments.

    },
    setValue : function(display, value, newAddress) { // Setting the textbox value
        console.log(display); // This returns undefined
        console.log(value); // This returns undefined
        if (display && display !== "" &&
            value && value !== "") {
            this.$element.val(this.updater(display)).change();
            this.$hiddenInput.val(value);
            this.$element.addClass("msuggest-selected");
        }
        if(newAddress === false){
            return this.hide();
        }
    },
});

Why does it listen to the function, but not the values passed to it? Do I need to include an extra line of code somewhere to define these arguments?

Anyone with jQuery experience would be of great help! This is bottlenecking progress on a current project. Thanks for your time!

EDIT: I've missed out the code of how the arguments are trying to be passed from the external script to the internal function of the plugin. Here is the plugin definition with how the external call is handled, can anyone see a problem with this?

$.fn.multisuggest = function(option) {
    return this.each(function() {
        var $this = $(this), data = $this.data('multisuggest'), options = typeof option === 'object' && option;
        if (!data) {
            $this.data('multisuggest', ( data = new MultiSuggest(this, options)));
        } else if (typeof(option) === 'string') {
            var method = data[option];
            var parameters = Array.prototype.slice.call(arguments, 1);

            method.apply(this, parameters);
        }
    });
};
役に立ちましたか?

解決

The "usual" plugin supervisor looks like this :

// *****************************
// ***** Start: Supervisor *****
$.fn.multisuggest = function( method ) {
    if ( methods[method] ) {
        return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || !method ) {
        return methods.init.apply( this, arguments );
    } else {
        $.error( 'Method ' + method + ' does not exist in jQuery.' + pluginName );
    }
};
// ***** Fin: Supervisor *****
// ***************************

All the looping through this should be inside the method functions, not in the supervisor.

I'm a little worried that new MultiSuggest(...) appears in the current supervisor. That sort of thing is totally unconventional. The original author clearly had something in mind.

他のヒント

You need to extend the jQuery plugin function which is attached to $.fn['multisuggest'], that function is probably only taking and passing one parameter.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top