src/js/serializers/PlatformSerializer.js
branchnew-model
changeset 864 5e76a06b961c
parent 860 7fd843e0dc4e
child 866 3bf7aa8216e5
equal deleted inserted replaced
860:7fd843e0dc4e 864:5e76a06b961c
     5 IriSP.serializers.platform = {
     5 IriSP.serializers.platform = {
     6     types :  {
     6     types :  {
     7         media : {
     7         media : {
     8             serialized_name : "medias",
     8             serialized_name : "medias",
     9             model_name : "media",
     9             model_name : "media",
    10             deserializer : function(_data, _container) {
    10             deserializer : function(_data, _source) {
    11                 var _res = new IriSP.Model.Media(_data.id, _container);
    11                 var _res = new IriSP.Model.Media(_data.id, _source);
    12                 _res.url = _data.href;
    12                 _res.url = _data.href;
    13                 _res.title = _data.meta["dc:title"];
    13                 _res.title = _data.meta["dc:title"];
    14                 _res.description = _data.meta["dc:description"];
    14                 _res.description = _data.meta["dc:description"];
    15                 _res.setDuration(_data.meta["dc:duration"]);
    15                 _res.setDuration(_data.meta["dc:duration"]);
    16                 return _res;        
    16                 return _res;        
    17             },
    17             },
    18             serializer : function(_data, _container) {
    18             serializer : function(_data, _source) {
    19                 return {
    19                 return {
    20                     id : _container.unNamespace(_data.id),
    20                     id : _source.unNamespace(_data.id),
    21                     href : _data.url,
    21                     href : _data.url,
    22                     meta : {
    22                     meta : {
    23                         "dc:title" : _data.title,
    23                         "dc:title" : _data.title,
    24                         "dc:description" : _data.description,
    24                         "dc:description" : _data.description,
    25                         "dc:duration" : _data.duration.milliseconds
    25                         "dc:duration" : _data.duration.milliseconds
    28             }
    28             }
    29         },
    29         },
    30         tag : {
    30         tag : {
    31             serialized_name : "tags",
    31             serialized_name : "tags",
    32             model_name : "tag",
    32             model_name : "tag",
    33             deserializer : function(_data, _container) {
    33             deserializer : function(_data, _source) {
    34                 var _res = new IriSP.Model.Tag(_data.id, _container);
    34                 var _res = new IriSP.Model.Tag(_data.id, _source);
    35                 _res.title = _data.meta["dc:title"];
    35                 _res.title = _data.meta["dc:title"];
    36                 return _res;        
    36                 return _res;        
    37             },
    37             },
    38             serializer : function(_data, _container) {
    38             serializer : function(_data, _source) {
    39                 return {
    39                 return {
    40                     id : _container.unNamespace(_data.id),
    40                     id : _source.unNamespace(_data.id),
    41                     meta : {
    41                     meta : {
    42                         "dc:title" : _data.title
    42                         "dc:title" : _data.title
    43                     }
    43                     }
    44                 }
    44                 }
    45             }
    45             }
    46         },
    46         },
    47         annotationTypes : {
    47         annotationType : {
    48             serialized_name : "annotation-types",
    48             serialized_name : "annotation-types",
    49             deserializer : function(_data, _container) {
    49             deserializer : function(_data, _source) {
    50                 var _res = new IriSP.Model.AnnotationType(_data.id, _container);
    50                 var _res = new IriSP.Model.AnnotationType(_data.id, _source);
    51                 _res.title = _data["dc:title"];
    51                 _res.title = _data["dc:title"];
    52                 _res.description = _data["dc:description"];
    52                 _res.description = _data["dc:description"];
    53                 return _res;        
    53                 return _res;        
    54             },
    54             },
    55             serializer : function(_data, _container) {
    55             serializer : function(_data, _source) {
    56                 return {
    56                 return {
    57                     id : _container.unNamespace(_data.id),
    57                     id : _source.unNamespace(_data.id),
    58                     "dc:title" : _data.title,
    58                     "dc:title" : _data.title,
    59                     "dc:description" : _data.description
    59                     "dc:description" : _data.description
    60                 }
    60                 }
    61             }
    61             }
    62         },
    62         },
    63         annotation : {
    63         annotation : {
    64             serialized_name : "annotations",
    64             serialized_name : "annotations",
    65             deserializer : function(_data, _container) {
    65             deserializer : function(_data, _source) {
    66                 var _res = new IriSP.Model.Annotation(_data.id, _container);
    66                 var _res = new IriSP.Model.Annotation(_data.id, _source);
    67                 _res.title = _data.content.title;
    67                 _res.title = _data.content.title;
    68                 _res.description = _data.content.description;
    68                 _res.description = _data.content.description;
    69                 _res.created = IriSP.Model.isoToDate(_data.meta["dc:created"]);
    69                 _res.created = IriSP.Model.isoToDate(_data.meta["dc:created"]);
    70                 var _c = parseInt(_data.color).toString(16);
    70                 var _c = parseInt(_data.color).toString(16);
    71                 while (_c.length < 6) {
    71                 while (_c.length < 6) {
    72                     _c = '0' + _c;
    72                     _c = '0' + _c;
    73                 }
    73                 }
    74                 _res.color = '#' + _c;
    74                 _res.color = '#' + _c;
    75                 _res.setMedia(_data.media, _container);
    75                 _res.setMedia(_data.media, _source);
    76                 _res.setAnnotationType(_data.meta["id-ref"]);
    76                 _res.setAnnotationType(_data.meta["id-ref"]);
    77                 _res.setTags(IriSP._(_data.tags).pluck("id-ref"));
    77                 _res.setTags(IriSP._(_data.tags).pluck("id-ref"));
    78                 _res.setBegin(_data.begin);
    78                 _res.setBegin(_data.begin);
    79                 _res.setEnd(_data.end);
    79                 _res.setEnd(_data.end);
       
    80                 _res.creator = _data.meta["dc:creator"];
    80                 return _res;
    81                 return _res;
    81             },
    82             },
    82             serializer : function(_data, _container) {
    83             serializer : function(_data, _source) {
    83                 return {
    84                 return {
    84                     id : _container.unNamespace(_data.id),
    85                     id : _source.unNamespace(_data.id),
    85                     content : {
    86                     content : {
    86                         title : _data.title,
    87                         title : _data.title,
    87                         description : _data.description
    88                         description : _data.description
    88                     },
    89                     },
    89                     media : _container.unNamespace(_data.media.contents),
    90                     media : _source.unNamespace(_data.media.contents),
    90                     meta : {
    91                     meta : {
    91                         "id-ref" : _container.unNamespace(_data.annotationType.contents),
    92                         "id-ref" : _source.unNamespace(_data.annotationType.contents),
    92                         "dc:created" : IriSP.Model.dateToIso(_data.created)
    93                         "dc:created" : IriSP.Model.dateToIso(_data.created),
       
    94                         "dc:creator" : _data.creator
    93                     },
    95                     },
    94                     tags : _data.getTags().map(function(_el, _id) {
    96                     tags : _data.getTags().map(function(_el, _id) {
    95                        return {
    97                        return {
    96                            "id-ref" : _container.unNamespace(_id)
    98                            "id-ref" : _source.unNamespace(_id)
    97                        } 
    99                        } 
    98                     })
   100                     })
    99                 }
   101                 }
   100             }
   102             }
   101         }
   103         }
   102     },
   104     },
   103     serialize : function(_container) {
   105     serialize : function(_source) {
   104         var _res = {},
   106         var _res = {},
   105             _this = this;
   107             _this = this;
   106         _container.each(function(_list, _typename) {
   108         _source.each(function(_list, _typename) {
   107             _res[_this.types[_typename].serialized_name] = _list.map(function(_el) {
   109             if (typeof _this.types[_typename] !== "undefined") {
   108                 return _this.types[_typename].serializer(_el, _container);
   110                 _res[_this.types[_typename].serialized_name] = _list.map(function(_el) {
   109             });
   111                     return _this.types[_typename].serializer(_el, _source);
       
   112                 });
       
   113             }
   110         });
   114         });
   111         return _res;
   115         return _res;
   112     },
   116     },
   113     deSerialize : function(_data, _container) {
   117     deSerialize : function(_data, _source) {
   114         IriSP._(this.types).each(function(_type, _typename) {
   118         IriSP._(this.types).each(function(_type, _typename) {
   115             if (typeof _data[_type.serialized_name] !== "undefined") {
   119             var _listdata = _data[_type.serialized_name];
   116                 var _list = new IriSP.Model.List(_container.directory);
   120             if (typeof _listdata !== "undefined") {
   117                 IriSP._(_data[_type.serialized_name]).each(function(_el) {
   121                 var _list = new IriSP.Model.List(_source.directory);
   118                     _list.addElement(_type.deserializer(_el, _container));
   122                 if (_listdata.hasOwnProperty("length")) {
   119                 });
   123                     var _l = _listdata.length;
   120                 _container.addList(_typename, _list);
   124                     for (var _i = 0; _i < _l; _i++) {
       
   125                         _list.addElement(_type.deserializer(_listdata[_i], _source));
       
   126                     }
       
   127                 } else {
       
   128                     _list.addElement(_type.deserializer(_listdata, _source));
       
   129                 }
       
   130                 _source.addList(_typename, _list);
   121             }
   131             }
   122         });
   132         });
   123         
   133         
   124         if (typeof _data.meta !== "undefined" && typeof _data.meta.main_media !== "undefined" && typeof _data.meta.main_media["id-ref"] !== "undefined") {
   134         if (typeof _data.meta !== "undefined" && typeof _data.meta.main_media !== "undefined" && typeof _data.meta.main_media["id-ref"] !== "undefined") {
   125             _container.setCurrentMediaId(_data.meta.main_media["id-ref"]);
   135             _source.setCurrentMediaId(_data.meta.main_media["id-ref"]);
   126         }
   136         }
   127         _container.setDefaultCurrentMedia();
   137         _source.setDefaultCurrentMedia();
   128     }
   138     }
   129 }
   139 }