client/js/full-json.js
author ymh <ymh.work@gmail.com>
Wed, 09 Sep 2015 18:37:15 +0200
changeset 499 7d374d366b28
parent 444 19f0b7803aed
child 516 7d1d44b53af5
permissions -rw-r--r--
Correct strange bug on fill of editor box with a difference of beavior between paperjs 0.9.23 and 0.9.24. Real cause not found, but this correction visually solve the problem

/* Saves the Full JSON at each modification */

Rkns.jsonIO = function(_renkan, _opts) {
    var _proj = _renkan.project;
    if (typeof _opts.http_method === "undefined") {
        _opts.http_method = 'PUT';
    }
    var _load = function() {
        _renkan.renderer.redrawActive = false;
        _proj.set({
            loadingStatus : true
        });
        Rkns.$.getJSON(_opts.url, function(_data) {
            _renkan.dataloader.load(_data);
            _proj.set({
                loadingStatus : false
            });
            _proj.set({
                saveStatus : 0
            });
            _renkan.renderer.redrawActive = true;
            _renkan.renderer.fixSize();
        });
    };
    var _save = function() {
        _proj.set({
            saveStatus : 2
        });
        var _data = _proj.toJSON();
        if (!_renkan.read_only) {
            Rkns.$.ajax({
                type : _opts.http_method,
                url : _opts.url,
                contentType : "application/json",
                data : JSON.stringify(_data),
                success : function(data, textStatus, jqXHR) {
                    _proj.set({
                        saveStatus : 0
                    });
                }
            });
        }

    };
    var _thrSave = Rkns._.throttle(function() {
        setTimeout(_save, 100);
    }, 1000);
    _proj.on("add:nodes add:edges add:users add:views", function(_model) {
        _model.on("change remove", function(_model) {
            _thrSave();
        });
        _thrSave();
    });
    _proj.on("change", function() {
        if (!(_proj.changedAttributes.length === 1 && _proj
                .hasChanged('saveStatus'))) {
            _thrSave();
        }
    });

    _load();
};