Armond Hambarchian
Armond Hambarchian

Reputation: 21

Callback is not a function error in selectize

Im working on a yii2-basic project and i used selectize extension for my forms. when i run this part it returns an error : callback is not a function

report_cp is a dropdown field that after selecting a value it will enable second field and sets a function with a url + selected value to report_cs.load

Here is JsFiddle link https://jsfiddle.net/6uhLts2w/5/ with replaced urls with test json server

    var report_cp; // all these are dropdown inputs
    var report_cs; // at start disabled, will enable after report_cp value selected
    var report_cnt; // at start disabled, will enable after report_cs value selected

    report_cp = $('#reports-cp').selectize({
        onChange: function(value) {
            if (!value.length) return;

            report_cs.enable();

            report_cs.load(function (query, callback) {
                if (!query.length) return callback();
                $.getJSON('../search/sections?id=' + value, { query: encodeURIComponent(query) }, function (data) { callback(data); })//error is right here
                .fail(function () { callback(); });
            });
        },
        load:function (query, callback) { //retrieve select options from database
                if (!query.length) return callback();
                $.getJSON('../search/companies', { query: encodeURIComponent(query) }, function (data) { callback(data); })
                .fail(function () { callback(); });
        },
        valueField: 'id',
        labelField: 'name',
        searchField: ['name'],
        persist: false,
        createOnBlur: true,
        maxItems: 1,
        create: true
    });

    report_cs = $('#reports-cs').selectize({
        onChange: function(value) {
            if (!value.length) return;

            report_cs.load(function (query, callback) {
                if (!query.length) return callback();
                $.getJSON('../search/contacts?id=' + value, { query: encodeURIComponent(query) }, function (data) { callback(data); report_cs.enable(); })
                .fail(function () { callback(); });
            });
        },
        valueField: 'id',
        labelField: 'name',
        searchField: ['name'],
        persist: false,
        createOnBlur: false,
        maxItems: 1,
        create: false
    });
    //report_cnt same as report_cs goes here...

Upvotes: 0

Views: 1101

Answers (1)

Torv
Torv

Reputation: 1234

Check documentation.

Loads options by invoking the provided function. The function should accept one argument (callback) and invoke the callback with the results once they are available.

so you should use something like report_cs.load(function (callback) {}...

Upvotes: 2

Related Questions