NodeJS + Mongoose + Backbone.Marionette嵌套Collection模板

时间:2013-04-03 18:23:58

标签: node.js marionette templating

所有 我正在开发一个存储我的多媒体目录的应用程序,我是这样的JSON集合:

    { "_id" : ObjectId( "5142f55394474e2aac000001" ),
      "contentType" : "binary/octet-stream",
      "length" : 2732376,
      "chunkSize" : 262144,
      "uploadDate" : Date( 1363342677601 ),
      "metadata" : { 
         "TIT2" : "Chase The Blues (Cameron McVey Remix)",
         "TPE1" : "Terranova",
         "TRCK" : "1/13",
         "TALB" : "!K7",
         "TPOS" : "1/1",
         "TDRC" : "2000-06",
         "TCON" : [ 
                   "Electronica", 
                   "Trip-Hop" ],
         "COMM" : [ 
                   "Chillout", 
                   "Love", 
                   "German", 
                   "Berlin", 
                   "2000s", 
                   "Female Vocalists", 
                   "Male Vocalists" ],
         "TMED" : "CD",
         "TMOO" : "Chill",
         "TDOR" : "2000-06",
         "TSO2" : "Various Artists",
         "TPE2" : "Various Artists",
         "TCMP" : "1",
         "TSOP" : "Terranova",
         "TIT1" : "Electronica",
         "TPUB" : "Sinedín Music",
         "TLAN" : "eng",
         "TYER" : [ 
                  "2000" ],
         },
      "md5" : "617401af615ac0c6cb1dee9a3f1b99e6",
      "origin" : "Chase The Blues.109eb5ab5105a1caa505a26657f7f9a8.mp3",
      "evolution" : null,
      "insertDate" : Date( 1336662308000 ),
      "tagSource" : "MusicBrainz",
      "mediainfo" : 
                  { "Format" : "MPEG Audio",
                    "Format version" : "Version 1",
                    "Format profile" : "Layer 3",
                    "Duration" : "3mn 47s",
                    "Bit rate mode" : "Constant",
                    "Bit rate" : "96.0 Kbps",
                    "Channel(s)" : "1 channel",
                    "Sampling rate" : "44.1 KHz",
                    "Compression mode" : "Lossy",
                    "Stream size" : "2.60 MiB (100%)",
                    "Language" : "English" 
                  }
        }

所以,正如您所看到的,文档中有“metadata”和“mediainfo”数组 在models.js中,在客户端,我重写了像这样的模型解析函数

    var Audio_Model = Backbone.Model.extend({

      idAttribute: "_id",
      url: 'AudioModel',
      urlRoot: 'AudioModel' ,

      parse: function(response) {

      // Check if response includes some nested collection data...
      if (_.has(response, 'metadata')){

         // Check if this model has a property called metadata
         if (!_.has(this, 'metadata')) {  // It does not...
             // So instantiate a collection and pass in raw data
             this.metadata = new Audio_Collection_Metadata(response.metadata);
         } else {
             // It does, so just reset the collection
             this.metadata.reset(response.metadata);
         }
         delete response.metadata;
      }
      // Check if response includes some nested collection data...
      if (_.has(response, 'mediainfo')){
             // Check if this model has a property called mediainfo
             if (!_.has(this, 'mediainfo')) {  // It does not...
             // So instantiate a collection and pass in raw data
             this.mediainfo = new Audio_Collection_Mediainfo(response.mediainfo);
         } else {
             // It does, so just reset the collection
             this.mediainfo.reset(response.mediainfo);
    }
    delete response.mediainfo;
   }
   return response;
   }
  });

所以我创建了两个单独的'元数据'和'mediainfo'集合 我的问题是如何在html模板中呈现'元数据'和'mediainfo',因为在'mediainfo'和'metadata'集合中,键,值不是固定的,在'元数据'中,一些键是值的数组和数组中的项目数不固定

我为这两个集合创建了backbone.marionette.itemview和compositeview,但我不知道如何渲染

Plase,有人有解决方案吗? 最诚挚的问候

1 个答案:

答案 0 :(得分:0)

最后我自己用数据规范化修复了这个问题,这是采用的新的mongoose模式:

    var TagSchema = new mongoose.Schema({
  value : {type : String, default: '', required: true}
    });

    var MetadataSchema = new mongoose.Schema({
 name  : {type: String, default: '', required : true},
 values: [TagSchema]
    });

    var MediainfoSchema = new mongoose.Schema({
 name : {type: String, default: ''},
 value: {type: String, default: ''}
    });

    var StreamSchema = new mongoose.Schema({
 _id: mongoose.Schema.ObjectId,
 TIT2: {type : String, default: '', required: true},
 metadata: [MetadataSchema],
 mediainfo:[MediainfoSchema]
   });

因此,使用CollectionView和CompositeView的序列,我可以浏览整个模型 希望这可以帮助某人