client/js/json-serializer.js
changeset 4 f5297dde9053
parent 2 3360c3f7fb18
child 5 67085e6281e5
equal deleted inserted replaced
3:7722ec70c01b 4:f5297dde9053
     1 Rkns.Serializers.BasicJson = Rkns.Utils.inherit(Rkns.Serializers._Base);
     1 Rkns.Serializers.BasicJson = Rkns.Utils.inherit(Rkns.Serializers._Base);
     2 
     2 
     3 Rkns.Serializers.BasicJson.prototype._init = function() {
     3 Rkns.Serializers.BasicJson.prototype._init = function() {
     4     this.load(this._project._opts.url);
     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);
     5 }
     9 }
     6 
    10 
     7 Rkns.Serializers.BasicJson.prototype.load = function(_url) {
    11 Rkns.Serializers.BasicJson.prototype.load = function(_url) {
     8     var _this = this;
    12     var _this = this;
     9     Rkns.$.getJSON(_url, function(_data) {
    13     Rkns.$.getJSON(_url, function(_data) {
    17         _serializedData = JSON.parse(_serializedData);
    21         _serializedData = JSON.parse(_serializedData);
    18     }
    22     }
    19     var _proj = this._project;
    23     var _proj = this._project;
    20     _proj.title = _serializedData.title || "(untitled project)";
    24     _proj.title = _serializedData.title || "(untitled project)";
    21     if (typeof _serializedData.users === "object" && _serializedData.users) {
    25     if (typeof _serializedData.users === "object" && _serializedData.users) {
    22         _proj.users.addElements(
    26         Rkns._(_serializedData.users).each(function(_data) {
    23             Rkns._(_serializedData.users).map(function(_data) {
    27             var _userData = {
    24                 var _userData = {
    28                 id: _data.id,
    25                     id: _data.id,
    29                 title: _data.title,
    26                     title: _data.title,
    30                 uri: _data.uri,
    27                     uri: _data.uri,
    31                 color: _data.color
    28                     color: _data.color
    32             };
    29                 };
    33             _proj.addUser(_userData);
    30                 return new Rkns.Model.User(_proj, _userData);
    34         });
    31             })
       
    32         );
       
    33     }
    35     }
    34     if (typeof _serializedData.nodes === "object" && _serializedData.nodes) {
    36     if (typeof _serializedData.nodes === "object" && _serializedData.nodes) {
    35         _proj.nodes.addElements(
    37         Rkns._(_serializedData.nodes).each(function(_data) {
    36             Rkns._(_serializedData.nodes).map(function(_data) {
    38             var _nodeData = {
    37                 var _nodeData = {
    39                 id: _data.id,
    38                     id: _data.id,
    40                 title: _data.title,
    39                     title: _data.title,
    41                 uri: _data.uri,
    40                     uri: _data.uri,
    42                 created_by: _data.created_by,
    41                     created_by: _data.created_by,
    43                 position: {
    42                     position: {
    44                     x: _data.position.x,
    43                         x: _data.position.x,
    45                     y: _data.position.y
    44                         y: _data.position.y
    46                 }
    45                         //x: 800 * Math.random() - 400,
    47             };
    46                         //y: 600 * Math.random() - 300
    48             _proj.addNode(_nodeData);
    47                     }
    49         });
    48                 };
       
    49                 return new Rkns.Model.Node(_proj, _nodeData);
       
    50             })
       
    51         );
       
    52     }
    50     }
    53     if (typeof _serializedData.edges === "object" && _serializedData.edges) {
    51     if (typeof _serializedData.edges === "object" && _serializedData.edges) {
    54         _proj.edges.addElements(
    52         Rkns._(_serializedData.edges).each(function(_data) {
    55             Rkns._(_serializedData.edges).map(function(_data) {
    53             var _edgeData = {
    56                 var _edgeData = _data;
    54                 id: _data.id,
    57                 return new Rkns.Model.Edge(_proj, _edgeData);
    55                 title: _data.title,
    58             })
    56                 uri: _data.uri,
    59         );
    57                 from: _data.from,
       
    58                 to: _data.to,
       
    59                 created_by: _data.created_by
       
    60             };
       
    61             _proj.addEdge(_edgeData);
       
    62         });
    60     }
    63     }
    61 }
    64 }
       
    65 
       
    66 Rkns.Serializers.BasicJson.prototype.serialize = function() {
       
    67     var _res = {
       
    68         title: this._project.title,
       
    69         users: this._project.users.map(function(_user) {
       
    70             return {
       
    71                 id: _user.id,
       
    72                 title: _user.title,
       
    73                 uri: _user.uri,
       
    74                 color: _user.color
       
    75             }
       
    76         }),
       
    77         nodes: this._project.nodes.map(function(_node) {
       
    78             return {
       
    79                 id: _node.id,
       
    80                 title: _node.title,
       
    81                 uri: _node.uri,
       
    82                 created_by: _node.created_by.id,
       
    83                 position: {
       
    84                     x: _node.position.x,
       
    85                     y: _node.position.y
       
    86                 }
       
    87             }
       
    88         }),
       
    89         edges: this._project.edges.map(function(_node) {
       
    90             return {
       
    91                 id: _node.id,
       
    92                 title: _node.title,
       
    93                 uri: _node.uri,
       
    94                 from: _node.from.id,
       
    95                 to: _node.to.id,
       
    96                 created_by: _node.created_by.id
       
    97             }
       
    98         })
       
    99     }
       
   100     return _res;
       
   101 }
       
   102 
       
   103 Rkns.Serializers.BasicJson.prototype._save = function() {
       
   104     var _data = this.serialize();
       
   105     Rkns.$.post(
       
   106         this._project._opts.url,
       
   107         _data,
       
   108         function(_res) {
       
   109         }
       
   110     );
       
   111 }