proxy.indexdb

webix.proxy.indexdb

webix.proxy.indexdb helper.

Please look into the linked official documentation.

References

helpers
bind(), delay().

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
webix.proxy.indexdb = {
    $proxy:true,
    create:function(db, config, version, callback){
        this.source = db + "/";
        this._get_db(callback, version, function(e){
            var db = e.target.result;
            for (var key in config){
                var data = config[key];
                var store = db.createObjectStore(key, { keyPath: "id", autoIncrement:true });
                for (var i = 0; i < data.length; i++)
                    store.put(data[i]);
            }
        });
    },
    _get_db:function(callback, version, upgrade){
        if (this.source.indexOf("/") != -1){
            var parts = this.source.split("/");
            this.source = parts[1];
            version = version || parts[2];

            var _index = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB;

            var db;
            if (version)
                db = _index.open(parts[0], version);
            else
                db = _index.open(parts[0]);

            if (upgrade)
                db.onupgradeneeded = upgrade;
            db.onerror = function(){ };
            db.onblocked = function(){ };
            db.onsuccess = webix.bind(function(e){
                this.db =  e.target.result;
                if (callback)
                    callback.call(this);
            },this);
        } else if (this.db)
            callback.call(this);
        else
            webix.delay(this._get_db, this, [callback], 50);
    },

    load:function(view, callback){
        this._get_db(function(){
            var store = this.db.transaction(this.source).objectStore(this.source);
            var data = [];

            store.openCursor().onsuccess = function(e) {
                var result = e.target.result;
                if(result){
                    data.push(result.value);
                    result["continue"]();
                } else {
                    view.parse(data);
                    webix.ajax.$callback(view, callback, "[]", data);
                }
            };
        });
    },
    save:function(view, update, dp, callback){
        this._get_db(function(){
            var mode = update.operation;
            var data = update.data;
            var id = update.id;

            var store = this.db.transaction([this.source], "readwrite").objectStore(this.source);

            var req;
            if (mode == "delete")
                req = store["delete"](id);
            else if (mode == "update")
                req = store.put(data);
            else if (mode == "insert"){
                delete data.id;
                req = store.add(data);
            }

            req.onsuccess = function(e) {
                var result = { status: mode, id:update.id };
                if (mode == "insert")
                    result.newid = e.target.result;
                dp.processResult(result, result);
            };
        });
    }
};