Sha256: b18d89f329b233f30debd27a305dbb219813b43aa55289fb4aaefb2ff611ce4a

Contents?: true

Size: 1.81 KB

Versions: 11

Compression:

Stored size: 1.81 KB

Contents

$.fn.alchemyCustomModelSelect = function(options) {

    return this.select2({
        placeholder: options.placeholder,
        allowClear: true,
        initSelection: function(element, callback) {
            console.log("pippo")
            var id = $(element).val();
            console.log(id)
            if (id !== "") {
                var q = {}
                if (options.query_params){
                    q = options.query_params()
                }
                $.ajax(options.url, {
                    dataType: "json",
                    data: {
                        id: id,
                        q: q,
                        per_page: 3000
                    }
                }).done(function(data) {
                    var el = null
                    $.each(data.results, function(index,element){
                        if (element.text == id){

                            el = element
                        }
                    })
                    callback(el);
                });
            }
        },
        ajax: {
            url: options.url,
            datatype: 'json',
            quietMillis: 300,
            data: function (term, page) {
                var extend_query_params = {}
                if (options.query_params) {

                    extend_query_params =  options.query_params(term,page)

                }
                return {
                    q: $.extend({
                        name: term
                    }, extend_query_params),
                    page: page
                }
            },
            results: function (data) {
                var meta = data.meta
                return {
                    results: data.results,
                    more: meta.page * meta.per_page < meta.total_count
                }
            }
         },

    })
}

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
alchemy-custom-model-3.2 app/assets/javascripts/alchemy-custom-model/alchemy_custom_model_select.js
alchemy-custom-model-3.1.5 app/assets/javascripts/alchemy-custom-model/alchemy_custom_model_select.js
alchemy-custom-model-3.1.4 app/assets/javascripts/alchemy-custom-model/alchemy_custom_model_select.js
alchemy-custom-model-3.1.3 app/assets/javascripts/alchemy-custom-model/alchemy_custom_model_select.js
alchemy-custom-model-3.1.2 app/assets/javascripts/alchemy-custom-model/alchemy_custom_model_select.js
alchemy-custom-model-3.1.1 app/assets/javascripts/alchemy-custom-model/alchemy_custom_model_select.js
alchemy-custom-model-3.1.0 app/assets/javascripts/alchemy-custom-model/alchemy_custom_model_select.js
alchemy-custom-model-2.2.2 app/assets/javascripts/alchemy-custom-model/alchemy_custom_model_select.js
alchemy-custom-model-3.0.0 app/assets/javascripts/alchemy-custom-model/alchemy_custom_model_select.js
alchemy-custom-model-2.2.1 app/assets/javascripts/alchemy-custom-model/alchemy_custom_model_select.js
alchemy-custom-model-2.2.0 app/assets/javascripts/alchemy-custom-model/alchemy_custom_model_select.js