DataLoader

class webix.DataLoader(data)
Arguments:
  • data (object) – A configuration object

Dataloader component.

References

helpers
ajax(), bind(), delay(), dp(), extend(), proto(), toArray(), toFunctor().
mixins
AtomDataLoader(), DataStore().

Referenced by

components
DataCollection().
views
chart(), datatable(), htmlform(), proto().

External references

Official documentation page.

Code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
webix.DataLoader=webix.proto({
    $init:function(config){
        //prepare data store
        config = config || "";

        //list of all active ajax requests
        this._ajax_queue = webix.toArray();

        this.data = new webix.DataStore();

        this.data.attachEvent("onClearAll",webix.bind(this._call_onclearall,this));
        this.data.attachEvent("onServerConfig", webix.bind(this._call_on_config, this));
        this.attachEvent("onDestruct", this._call_onclearall);

        this.data.feed = this._feed;
        this.data.owner = config.id;
    },
    _feed:function(from,count,callback){
                //allow only single request at same time
                if (this._load_count)
                    return (this._load_count=[from,count,callback]);    //save last ignored request
                else
                    this._load_count=true;
                this._feed_last = [from, count];
                this._feed_common.call(this, from, count, callback);
    },
    _feed_common:function(from, count, callback, url, details){
        var state = null,
            url = url || this.data.url;

        var final_callback = [
            { success: this._feed_callback, error: this._feed_callback },
            callback
        ];

        if (from<0) from = 0;

        if(!details)
            details = { start: from, count:count };

        if(this.count())
            details["continue"] = "true";

        if (this.getState)
            state = this.getState();

        // proxy
        if (url && typeof url != "string"){
            if (state){
                if (state.sort)
                    details.sort = state.sort;
                if (state.filter)
                    details.filter = state.filter;
            }
            this.load(url, final_callback, details);
        } else { // GET
            url = url+((url.indexOf("?")==-1)?"?":"&");

            var params = [];
            for(var d in details){
                params.push(d+"="+details[d]);
            }
            if (state){
                if (state.sort)
                    params.push("sort["+state.sort.id+"]="+encodeURIComponent(state.sort.dir));
                if (state.filter)
                    for (var key in state.filter){
                        var filterValue = state.filter[key];
                        if(typeof filterValue == "object")
                            filterValue = webix.ajax().stringify(filterValue); //server daterangefilter
                        params.push("filter["+key+"]="+encodeURIComponent(filterValue));
                    }
            }

            url += params.join("&");

            this.load(url, final_callback);
        }
    },
    _feed_callback:function(){
        //after loading check if we have some ignored requests
        var temp = this._load_count;
        var last = this._feed_last;
        this._load_count = false;
        if (typeof temp =="object" && (temp[0]!=last[0] || temp[1]!=last[1]))
            this.data.feed.apply(this, temp);    //load last ignored request
    },
    //loads data from external URL
    load:function(url,call){
        var url = webix.proxy.$parse(url);
        var ajax = webix.AtomDataLoader.load.apply(this, arguments);

        //prepare data feed for dyn. loading
        if (!this.data.url)
            this.data.url = url;

        return ajax;
    },
    //load next set of data rows
    loadNext:function(count, start, callback, url, now){
        var config = this._settings;
        if (config.datathrottle && !now){
            if (this._throttle_request)
                window.clearTimeout(this._throttle_request);
            this._throttle_request = webix.delay(function(){
                this.loadNext(count, start, callback, url, true);
            },this, 0, config.datathrottle);
            return;
        }

        if (!start && start !== 0) start = this.count();
        if (!count)
            count = config.datafetch || this.count();

        this.data.url = this.data.url || url;
        if (this.callEvent("onDataRequest", [start,count,callback,url]) && this.data.url)
            this.data.feed.call(this, start, count, callback);
    },
    _maybe_loading_already:function(count, from){
        var last = this._feed_last;
        if(this._load_count && last){
            if (last[0]<=from && (last[1]+last[0] >= count + from )) return true;
        }
        return false;
    },
    removeMissed_setter:function(value){
        return (this.data._removeMissed = value);
    },
    //init of dataprocessor delayed after all settings processing
    //because it need to be the last in the event processing chain
    //to get valid validation state
    _init_dataprocessor:function(){
        var url = this._settings.save;

        if (url === true)
            url = this._settings.save = this._settings.url;

        var obj = { master: this };

        if (url && url.url)
            webix.extend(obj, url);
        else
            obj.url = url;

        webix.dp(obj);
    },
    save_setter:function(value){
        if (value)
            this.$ready.push(this._init_dataprocessor);

        return value;
    },
    scheme_setter:function(value){
        this.data.scheme(value);
    },
    dataFeed_setter:function(value){
        value = webix.proxy.$parse(value);

        this.data.attachEvent("onBeforeFilter", webix.bind(function(text, filtervalue){
            //complex filtering, can't be routed to dataFeed
            if (typeof text == "function") return true;

            //we have dataFeed and some text
            if (this._settings.dataFeed && (text || filtervalue)){
                text = text || "id";
                if (filtervalue && typeof filtervalue == "object")
                        filtervalue = filtervalue.id;

                this.clearAll();
                var url = this._settings.dataFeed;

                //js data feed
                if (typeof url == "function"){
                    var filter = {};
                    filter[text] = filtervalue;
                    url.call(this, filtervalue, filter);
                } else if (url.$proxy) {
                    if (url.load){
                        var filterobj = {}; filterobj[text] = filtervalue;
                        url.load(this, {
                            success: this._onLoad,
                            error: this._onLoadError
                        }, { filter: filterobj });
                    }
                } else {
                //url data feed
                    var urldata = "filter["+text+"]="+encodeURIComponent(filtervalue);
                    this.load(url+(url.indexOf("?")<0?"?":"&")+urldata, this._settings.datatype);
                }
                return false;
            }
        },this));
        return value;
    },
    _call_onready:function(){
        if (this._settings.ready && !this._ready_was_used){
            var code = webix.toFunctor(this._settings.ready, this.$scope);
            if (code)
                webix.delay(code, this, arguments);
            if (this.callEvent)
                webix.delay(this.callEvent, this, ["onReady", []]);
            this._ready_was_used = true;
        }
    },
    _call_onclearall:function(){
        for (var i = 0; i < this._ajax_queue.length; i++){
            var xhr = this._ajax_queue[i];

            //IE9 and IE8 deny extending of ActiveX wrappers
            try { xhr.aborted = true; } catch(e){
                webix._xhr_aborted.push(xhr);
            }
            xhr.abort();
        }
        this._load_count = false;
        this._ajax_queue = webix.toArray();
        this.waitData = webix.promise.defer();
    },
    _call_on_config:function(config){
        this._parseSeetingColl(config);
    }
},webix.AtomDataLoader);

//ie8 compatibility