server/bo_client/app/models/document.js
changeset 28 b0b56e0f8c7f
parent 20 a9b98b16b053
child 32 9765cf7cf817
equal deleted inserted replaced
27:a2342f26c9de 28:b0b56e0f8c7f
     5 export default DS.Model.extend({
     5 export default DS.Model.extend({
     6   //id: DS.attr('string'),
     6   //id: DS.attr('string'),
     7   uri: DS.attr('string'),
     7   uri: DS.attr('string'),
     8 
     8 
     9   title: DS.attr('string'),
     9   title: DS.attr('string'),
       
    10 
       
    11   language: DS.attr('string'),
    10 
    12 
    11   publishers: DS.attr({defaultValue: []}),
    13   publishers: DS.attr({defaultValue: []}),
    12 
    14 
    13   contributors: DS.attr({defaultValue: []}),
    15   contributors: DS.attr({defaultValue: []}),
    14 
    16 
    27     if(mp3) {
    29     if(mp3) {
    28       res.unshift(mp3);
    30       res.unshift(mp3);
    29     }
    31     }
    30     return res;
    32     return res;
    31   }),
    33   }),
       
    34 
       
    35   addContributor: function(contrib_def) {
       
    36     var contributors = this.get('contributors');
       
    37     if(_.findIndex(contributors, function(c) { return _.isEqual(c, contrib_def);}) < 0) {
       
    38       contributors.pushObject(contrib_def);
       
    39     }
       
    40     // must set dirty
       
    41     this.set('contributors', _.clone(this.get('contributors')));
       
    42   },
       
    43   removeContributor: function(contrib_def) {
       
    44     var contributors = this.get('contributors');
       
    45     var index = _.findIndex(contributors, function(c) { return _.isEqual(c, contrib_def);});
       
    46     if(index >= 0) {
       
    47       contributors.removeAt(index);
       
    48     }
       
    49     //must set dirty
       
    50     this.set('contributors', _.clone(this.get('contributors')));
       
    51   },
       
    52   saveContributor: function(contrib_def, index) {
       
    53     var contributors = this.get('contributors');
       
    54     if(index < 0 || index >= contributors.length) {
       
    55       return;
       
    56     }
       
    57 
       
    58     var contrib_def_map = {name: contrib_def.name||"", url: contrib_def.url||"", role: contrib_def.role||""};
       
    59     var existingContribs = _.filter(contributors, function(c, i) {
       
    60       return (i!==index && _.isEqual({name: c.name||"", url: c.url||"", role: c.role||""}, contrib_def_map));
       
    61     });
       
    62     if(existingContribs.length > 0) {
       
    63       // contributor exists, remove contributor @ index
       
    64       contributors.removeAt(index);
       
    65     }
       
    66     else {
       
    67       contributors[index] = contrib_def;
       
    68     }
       
    69     //must set dirty only if needed
       
    70     this.set('contributors', _.clone(this.get('contributors')));
       
    71   }
       
    72 
    32 });
    73 });