client/js/full-json.js
author veltr
Tue, 21 Aug 2012 12:46:11 +0200
changeset 23 70c8af9b44ec
parent 20 bd58970ffd16
child 24 121a24be9da4
permissions -rw-r--r--
Rebased Rendering on Backbone Model

/* 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() {
        Rkns.$.getJSON(_opts.url, function(_data) {
            _proj.get("users").add(_data.users);
            Rkns._(_data.nodes).each(function(_item) {
                _proj.addNode(_item);
            });
            Rkns._(_data.edges).each(function(_item) {
                _proj.addEdge(_item);
            });
        });
    }
    var _save = function() {
        var _data = {
            users: _proj.get("users").map(function(_item) {
                return {
                    id: _item.get("id"),
                    title: _item.get("title"),
                    uri: _item.get("uri"),
                    description: _item.get("description"),
                    color: _item.get("color")
                }
            }),
            nodes: _proj.get("nodes").map(function(_item) {
                return {
                    id: _item.get("id"),
                    title: _item.get("title"),
                    uri: _item.get("uri"),
                    description: _item.get("description"),
                    position: _item.get("position"),
                    created_by: _item.get("created_by").get("id")
                }
            }),
            edges: _proj.get("edges").map(function(_item) {
                return {
                    id: _item.get("id"),
                    title: _item.get("title"),
                    uri: _item.get("uri"),
                    description: _item.get("description"),
                    from: _item.get("from").get("id"),
                    to: _item.get("to").get("id"),
                    created_by: _item.get("created_by").get("id")
                }
            })
        };
        Rkns.$.ajax({
            type: _opts.http_method,
            url: _opts.url,
            contentType: "application/json",
            data: JSON.stringify(_data),
            success: function(data, textStatus, jqXHR) {
            }
        });
        
    }
    var _thrSave = Rkns._.throttle(_save, 2000);
    _load();
    _proj.on("add:nodes add:edges add:users", function(_model) {
        _model.on("change remove", function(_model) {
            _thrSave();
        });
    });
}