为什么在这个MarionetteJS复合视图中没有呈现元信息?

时间:2013-04-21 22:09:19

标签: backbone.js marionette

我遇到了使用MarionetteJS的CompositeView加载元信息的意外行为。服务器端的JSON如下所示:

{
    "meta": {
        "total": 2
    },
    "movies": [
        {
            "category": "Action",
            "description": "Another story goes ....",
            "id": 1,
            "stars": "4.0",
            "title": "My test movie"
        },
        {
            "category": "Action",
            "description": "the story goes....",
            "id": 2,
            "stars": null,
            "title": "my second movie"
        }
    ]
}

我使用Backbone集合来设置元信息,如:

MA.Collections.Movies = Backbone.Collection.extend({
  url: '/api/movies.json',
  parse: function(data) {
    this.meta = new MA.Models.MetaMovie(data.meta);
    return data.movies;
  }
});

MA.addInitializer(function() {
  MA.Collections.Movies.model = MA.Models.Movie;
})

但是当我尝试从CompositeView访问这个元变量时,集合被正确初始化,而元信息似乎丢失了。有关如何将此元信息提供给CompositeView模型的任何想法吗?

PS控制台输出如下:

composite view output

1 个答案:

答案 0 :(得分:1)

从fetch设置回调并使用它将meta.total的值直接设置到metaMovie模型实例

<强> movies.js:

MA.addInitializer(function(){
  var self = this;
  var metaMovie = new MA.Models.MetaMovie({total: 1});

  MA.collections.movies = new MA.Collections.Movies();
  MA.composites.movies = new MA.Views.Composites.Movies({
    itemView: MA.Views.Items.Movie,
    model: metaMovie,
    collection: MA.collections.movies
  });
  var p = MA.collections.movies.fetch();

  // see http://api.jquery.com/jQuery.ajax/ for details
  p.done(function(data) {
    self.metaMovie.set({total: data.total});
  });
});