BindSource

class webix.BindSource()

Bindsource mixin

Referenced by

mixins
BaseBind().
components
DataCollection().

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
webix.BindSource = {
    $init:function(){
        this._bind_hash = {};        //rules per target
        this._bind_updated = {};    //update flags
        this._ignore_binds = {};

        //apply specific bind extension
        this._bind_specific_rules(this);
    },
    saveBatch:function(code){
        this._do_not_update_binds = true;
        code.call(this);
        this._do_not_update_binds = false;
        this._update_binds();
    },
    setBindData:function(data, key){
        //save called, updating master data
        if (key)
            this._ignore_binds[key] = true;

        if (webix.debug_bind)
                webix.log("[bind:save] "+this.name+"@"+this._settings.id+" <= "+"@"+key);
        if (this.setValue)
            this.setValue(data);
        else if (this.setValues)
            this.setValues(data);
        else {
            var id = this.getCursor();
            if (id)
                this.updateItem(id, data);
            else
                this.add(data);
        }
        this.callEvent("onBindUpdate", [data, key]);
        if (this.save)
            this.save();

        if (key)
            this._ignore_binds[key] = false;
    },
    //fill target with data
    getBindData:function(key, update){
        //fire only if we have data updates from the last time
        if (this._bind_updated[key]) return false;
        var target = webix.$$(key);
        //fill target only when it visible
        if (target.isVisible(target._settings.id)){
            this._bind_updated[key] = true;
            if (webix.debug_bind)
                webix.log("[bind:request] "+this.name+"@"+this._settings.id+" => "+target.name+"@"+target._settings.id);
            this._bind_update(target, this._bind_hash[key][0], this._bind_hash[key][1]); //trigger component specific updating logic
            if (update && target.filter)
                target.refresh();
        }
    },
    //add one more bind target
    addBind:function(source, rule, format){
        this._bind_hash[source] = [rule, format];
    },
    removeBind:function(source){
        delete this._bind_hash[source];
        delete this._bind_updated[source];
        delete this._ignore_binds[source];
    },
    //returns true if object belong to "collection" type
    _bind_specific_rules:function(obj){
        if (obj.filter)
            webix.extend(this, webix.CollectionBind);
        else if (obj.setValue)
            webix.extend(this, webix.ValueBind);
        else
            webix.extend(this, webix.RecordBind);
    },
    //inform all binded objects, that source data was updated
    _update_binds:function(){
        if (!this._do_not_update_binds)
            for (var key in this._bind_hash){
                if (this._ignore_binds[key]) continue;
                this._bind_updated[key] = false;
                this.getBindData(key, true);
            }
    },
    //copy data from source to the target
    _bind_update_common:function(target, rule, data){
        if (target.setValue)
            target.setValue((data&&rule)?data[rule]:data);
        else if (!target.filter){
            if (!data && target.clear)
                target.clear();
            else {
                if (target._check_data_feed(data))
                    target.setValues(webix.clone(data));
            }
        } else {
            target.data.silent(function(){
                this.filter(rule,data);
            });
        }
        target.callEvent("onBindApply", [data,rule,this]);
    }
};


//pure data objects