0

I have created a simple JQuery extension :

(function($) {
    $.fn.extend({
        GMselect: function(options){ 
            var defaults = {
                url: '',
                onSelect: function(){},
                loadData: function(url){
                    $.get(url || this.url).done(function(res){
                       var opts = '';
                       for (i in res){
                           opts += '<option value='+res[i].id+'>'+res[i].name + '</option>';
                       }
                       me.html(opts); 
                    });
                }
            };
            var options = $.extend(defaults, options);
            options.loadData();
            $(this).change( options.onSelect );
            var me = $(this);
        }
    });
}(jQuery));

this way I can easly create select elements reading data from remote JSON source like this:

$('#mySelect').GMselect({
    url: 'getData'
});

This part is clear, however I would like to know the way to dynamically reload the select, by invoking the method like

$('#mySelect').reload(url)

Any help and comments will be appreciate

1

1 Answer 1

1

Well I think this could work basically making defaults a private object of the plugin to be able to play with it in all the functions (reuse the loadData function).

(function($) {

            var defaults = {
                url: '',
                onSelect: function(){},
                loadData: function(me,url){
                $.get(url || this.url).done(function(res){
                       var opts = '';
                       for (i in res){
                           opts += '<option value='+res[i].id+'>'+res[i].name + '</option>';
                       }
                       me.html(opts); 
                    });
                }
            };
            $.fn.extend({
                GMselect: function(options){ 
                    var me = $(this);
                    var options = $.extend(defaults, options);
                    options.loadData(me);
                    $(this).change( options.onSelect );
                },
                    reload:function(url){
                    var me = $(this);
                    defaults.loadData(me,url)
                }
            });
    }(jQuery));

Call with GMSelect:

$("#mySelect").GMselect({url:"JSON.json"})

JSON.json file:

[{"id":10,"name":"Apple"},{"id":45,"name":"Melon"},{"id":12,"name":"Kiwi"}]

Result

Result

Call with reload:

$("#mySelect").reload("JSON2.json")

JSON2.json file:

[{"id":10,"name":"RED"},{"id":45,"name":"PURPLE"},{"id":12,"name":"BLACK"}]

Result

Result

Basically the changes made were to add the function reload and make defaults private inside the plugin to use it in all the functions available in this case the defaults.loadData(me,url) inside the function reload.

Sign up to request clarification or add additional context in comments.

1 Comment

@user3460254 Feel free to mark the question as accepted if it was the one that helped you. Have a nice day

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.