server/src/main/webapp/js/json-serializer.js
changeset 13 6d0e0100f4e8
parent 9 2ab67fa1e78d
child 14 f7d9fe3ef6f3
equal deleted inserted replaced
9:2ab67fa1e78d 13:6d0e0100f4e8
     1 Rkns.Serializers.BasicJson = Rkns.Utils.inherit(Rkns.Serializers._Base);
       
     2 
       
     3 Rkns.Serializers.BasicJson.prototype._init = function() {
       
     4     this.load(this._project._opts.url);
       
     5     var _this = this;
       
     6     this.save = Rkns._.throttle(function() {
       
     7         _this._save.apply(this, Array.prototype.slice.call(arguments,0));
       
     8     }, 2000);
       
     9 }
       
    10 
       
    11 Rkns.Serializers.BasicJson.prototype.load = function(_url) {
       
    12     var _this = this;
       
    13     Rkns.$.getJSON(_url, function(_data) {
       
    14         _this.deserialize(_data);
       
    15         _this.handleCallbacks();
       
    16     });
       
    17 }
       
    18 
       
    19 Rkns.Serializers.BasicJson.prototype.deserialize = function(_serializedData) {
       
    20     if (typeof _serializedData === "string") {
       
    21         _serializedData = JSON.parse(_serializedData);
       
    22     }
       
    23     var _proj = this._project;
       
    24     _proj.title = _serializedData.title || "(untitled project)";
       
    25     if (typeof _serializedData.users === "object" && _serializedData.users) {
       
    26         Rkns._(_serializedData.users).each(function(_data) {
       
    27             var _userData = {
       
    28                 id: _data.id,
       
    29                 title: _data.title,
       
    30                 uri: _data.uri,
       
    31                 color: _data.color
       
    32             };
       
    33             _proj.addUser(_userData);
       
    34         });
       
    35     }
       
    36     if (typeof _serializedData.nodes === "object" && _serializedData.nodes) {
       
    37         Rkns._(_serializedData.nodes).each(function(_data) {
       
    38             var _nodeData = {
       
    39                 id: _data.id,
       
    40                 title: _data.title,
       
    41                 description: _data.description,
       
    42                 uri: _data.uri,
       
    43                 created_by: _data.created_by,
       
    44                 position: {
       
    45                     x: _data.position.x,
       
    46                     y: _data.position.y
       
    47                 }
       
    48             };
       
    49             _proj.addNode(_nodeData);
       
    50         });
       
    51     }
       
    52     if (typeof _serializedData.edges === "object" && _serializedData.edges) {
       
    53         Rkns._(_serializedData.edges).each(function(_data) {
       
    54             var _edgeData = {
       
    55                 id: _data.id,
       
    56                 title: _data.title,
       
    57                 uri: _data.uri,
       
    58                 from: _data.from,
       
    59                 to: _data.to,
       
    60                 created_by: _data.created_by
       
    61             };
       
    62             _proj.addEdge(_edgeData);
       
    63         });
       
    64     }
       
    65 }
       
    66 
       
    67 Rkns.Serializers.BasicJson.prototype.serialize = function() {
       
    68     var _res = {
       
    69         title: this._project.title,
       
    70         users: this._project.users.map(function(_user) {
       
    71             return {
       
    72                 id: _user.id,
       
    73                 title: _user.title,
       
    74                 uri: _user.uri,
       
    75                 color: _user.color
       
    76             }
       
    77         }),
       
    78         nodes: this._project.nodes.map(function(_node) {
       
    79             return {
       
    80                 id: _node.id,
       
    81                 title: _node.title,
       
    82                 uri: _node.uri,
       
    83                 created_by: _node.created_by.id,
       
    84                 position: {
       
    85                     x: _node.position.x,
       
    86                     y: _node.position.y
       
    87                 }
       
    88             }
       
    89         }),
       
    90         edges: this._project.edges.map(function(_node) {
       
    91             return {
       
    92                 id: _node.id,
       
    93                 title: _node.title,
       
    94                 description: _node.description,
       
    95                 uri: _node.uri,
       
    96                 from: _node.from.id,
       
    97                 to: _node.to.id,
       
    98                 created_by: _node.created_by.id
       
    99             }
       
   100         })
       
   101     }
       
   102     return _res;
       
   103 }
       
   104 
       
   105 Rkns.Serializers.BasicJson.prototype._save = function() {
       
   106     var _data = JSON.stringify(this.serialize());
       
   107     Rkns.$.ajax({
       
   108     	type: 'PUT',
       
   109     	url: this._project._opts.url,
       
   110     	contentType: "application/json",
       
   111     	data: _data,
       
   112     	success: function(data, textStatus, jqXHR) {
       
   113         }
       
   114     });
       
   115 };