client/js/full-json.js
author rougeronj
Tue, 15 Sep 2015 12:24:58 +0200
changeset 521 0d9b3f1b97e7
parent 516 7d1d44b53af5
child 543 5f7bebdcfc0d
permissions -rw-r--r--
Create new view id the current one isn't in the project or if it's the first one (so we let the first one always unchanged) Otherwise modify the current one
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20
bd58970ffd16 Refactoring to better fit the MVVM pattern
veltr
parents: 14
diff changeset
     1
/* Saves the Full JSON at each modification */
bd58970ffd16 Refactoring to better fit the MVVM pattern
veltr
parents: 14
diff changeset
     2
23
70c8af9b44ec Rebased Rendering on Backbone Model
veltr
parents: 20
diff changeset
     3
Rkns.jsonIO = function(_renkan, _opts) {
70c8af9b44ec Rebased Rendering on Backbone Model
veltr
parents: 20
diff changeset
     4
    var _proj = _renkan.project;
293
fba23fde14ba Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents: 282
diff changeset
     5
    if (typeof _opts.http_method === "undefined") {
20
bd58970ffd16 Refactoring to better fit the MVVM pattern
veltr
parents: 14
diff changeset
     6
        _opts.http_method = 'PUT';
bd58970ffd16 Refactoring to better fit the MVVM pattern
veltr
parents: 14
diff changeset
     7
    }
23
70c8af9b44ec Rebased Rendering on Backbone Model
veltr
parents: 20
diff changeset
     8
    var _load = function() {
307
eaadfa988db4 try to improve renderer
ymh <ymh.work@gmail.com>
parents: 293
diff changeset
     9
        _renkan.renderer.redrawActive = false;
414
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    10
        _proj.set({
443
4c7ab16e5845 best coding convention for loadingstatus and savingstatus, rempve loadingatatus form export
ymh <ymh.work@gmail.com>
parents: 414
diff changeset
    11
            loadingStatus : true
414
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    12
        });
23
70c8af9b44ec Rebased Rendering on Backbone Model
veltr
parents: 20
diff changeset
    13
        Rkns.$.getJSON(_opts.url, function(_data) {
444
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents: 443
diff changeset
    14
            _renkan.dataloader.load(_data);
414
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    15
            _proj.set({
443
4c7ab16e5845 best coding convention for loadingstatus and savingstatus, rempve loadingatatus form export
ymh <ymh.work@gmail.com>
parents: 414
diff changeset
    16
                loadingStatus : false
414
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    17
            });
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    18
            _proj.set({
443
4c7ab16e5845 best coding convention for loadingstatus and savingstatus, rempve loadingatatus form export
ymh <ymh.work@gmail.com>
parents: 414
diff changeset
    19
                saveStatus : 0
414
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    20
            });
307
eaadfa988db4 try to improve renderer
ymh <ymh.work@gmail.com>
parents: 293
diff changeset
    21
            _renkan.renderer.redrawActive = true;
4
f5297dde9053 Can now add nodes/edges
veltr
parents: 2
diff changeset
    22
        });
170
603ffa4c6fa5 correct ";" and "," in javascripts
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    23
    };
114
110f99eb417e moved options to defaults.js and improved read-only switching
veltr
parents: 105
diff changeset
    24
    var _save = function() {
414
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    25
        _proj.set({
443
4c7ab16e5845 best coding convention for loadingstatus and savingstatus, rempve loadingatatus form export
ymh <ymh.work@gmail.com>
parents: 414
diff changeset
    26
            saveStatus : 2
414
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    27
        });
114
110f99eb417e moved options to defaults.js and improved read-only switching
veltr
parents: 105
diff changeset
    28
        var _data = _proj.toJSON();
110f99eb417e moved options to defaults.js and improved read-only switching
veltr
parents: 105
diff changeset
    29
        if (!_renkan.read_only) {
160
408da84d4dc0 Convert tabs added by ide configuration mistake
veltr
parents: 116
diff changeset
    30
            Rkns.$.ajax({
414
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    31
                type : _opts.http_method,
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    32
                url : _opts.url,
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    33
                contentType : "application/json",
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    34
                data : JSON.stringify(_data),
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    35
                success : function(data, textStatus, jqXHR) {
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    36
                    _proj.set({
443
4c7ab16e5845 best coding convention for loadingstatus and savingstatus, rempve loadingatatus form export
ymh <ymh.work@gmail.com>
parents: 414
diff changeset
    37
                        saveStatus : 0
414
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    38
                    });
160
408da84d4dc0 Convert tabs added by ide configuration mistake
veltr
parents: 116
diff changeset
    39
                }
408da84d4dc0 Convert tabs added by ide configuration mistake
veltr
parents: 116
diff changeset
    40
            });
62
f9019462465a Publishing code is now same as Editing code with renkan.read_only = true
veltr
parents: 36
diff changeset
    41
        }
293
fba23fde14ba Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents: 282
diff changeset
    42
170
603ffa4c6fa5 correct ";" and "," in javascripts
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    43
    };
414
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    44
    var _thrSave = Rkns._.throttle(function() {
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    45
        setTimeout(_save, 100);
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    46
    }, 1000);
516
7d1d44b53af5 small fixes to be sure the project and the view are loaded before using it
rougeronj
parents: 444
diff changeset
    47
    
7d1d44b53af5 small fixes to be sure the project and the view are loaded before using it
rougeronj
parents: 444
diff changeset
    48
    //TODO: Rearrange to avoid the 2 firts PUT due to a change in the project model
7d1d44b53af5 small fixes to be sure the project and the view are loaded before using it
rougeronj
parents: 444
diff changeset
    49
    // Take car of setting up the listener correctly to listen the save action on the view
282
12ee99b182cd enhance view management.
cavaliet
parents: 170
diff changeset
    50
    _proj.on("add:nodes add:edges add:users add:views", function(_model) {
114
110f99eb417e moved options to defaults.js and improved read-only switching
veltr
parents: 105
diff changeset
    51
        _model.on("change remove", function(_model) {
23
70c8af9b44ec Rebased Rendering on Backbone Model
veltr
parents: 20
diff changeset
    52
            _thrSave();
70c8af9b44ec Rebased Rendering on Backbone Model
veltr
parents: 20
diff changeset
    53
        });
114
110f99eb417e moved options to defaults.js and improved read-only switching
veltr
parents: 105
diff changeset
    54
        _thrSave();
110f99eb417e moved options to defaults.js and improved read-only switching
veltr
parents: 105
diff changeset
    55
    });
110f99eb417e moved options to defaults.js and improved read-only switching
veltr
parents: 105
diff changeset
    56
    _proj.on("change", function() {
414
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    57
        if (!(_proj.changedAttributes.length === 1 && _proj
443
4c7ab16e5845 best coding convention for loadingstatus and savingstatus, rempve loadingatatus form export
ymh <ymh.work@gmail.com>
parents: 414
diff changeset
    58
                .hasChanged('saveStatus'))) {
414
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    59
            _thrSave();
276042cb477c correct fix size, improve control on view save button and related init state
ymh <ymh.work@gmail.com>
parents: 370
diff changeset
    60
        }
114
110f99eb417e moved options to defaults.js and improved read-only switching
veltr
parents: 105
diff changeset
    61
    });
293
fba23fde14ba Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents: 282
diff changeset
    62
62
f9019462465a Publishing code is now same as Editing code with renkan.read_only = true
veltr
parents: 36
diff changeset
    63
    _load();
170
603ffa4c6fa5 correct ";" and "," in javascripts
ymh <ymh.work@gmail.com>
parents: 160
diff changeset
    64
};