3

コレクションがモデルをフェッチしてからアンダースコアビットをレンダリングするまでバックボーンに待機するように指示する方法を知りたいです。

コンソールで、アンダースコア テンプレートからフィールドが欠落しているというエラーが返されます。console.log(this.collection.toJSON()) を実行すると、データが表示されません。したがって、データがフェッチされる前にビューがレンダリングされると思います。ビューがフェッチされるまで待機するようにするにはどうすればよいですか?

/////// 意見////////

define([
  'jquery',
  'underscore',
  'backbone',
  'collections/mitarbeiter',
  'text!/aquilamus/templates/mitarbeiter/mitarbeiter.html'
], function($, _, Backbone, MitarbeiterCollection, MitarbeiterTemplate){



 var MitarbeiterListView = Backbone.View.extend({
    el: $("#container"),
    initialize: function(){
      this.collection = new MitarbeiterCollection;
      this.collection.fetch();
      var newtemplate = MitarbeiterTemplate;
      this.template = _.template($(newtemplate).html());
    },
    render: function(){
      var renderedContent = this.template(this.collection.toJSON());

      $(this.el).html(renderedContent);
      return this;
    }


  });
  // Our module now returns our view
  return MitarbeiterListView;
});
4

5 に答える 5

11

他の提案された作品と同じように使用しresetますが、ここに代替手段があります。

define([
  'jquery',
  'underscore',
  'backbone',
  'collections/mitarbeiter',
  'text!/aquilamus/templates/mitarbeiter/mitarbeiter.html'
], function($, _, Backbone, MitarbeiterCollection, MitarbeiterTemplate){

 var MitarbeiterListView = Backbone.View.extend({
    el: $("#container"),
    initialize: function(){
      this.collection = new MitarbeiterCollection;

      var newtemplate = MitarbeiterTemplate;
      this.template = _.template($(newtemplate).html());
    },
    render: function(){
      var self = this;

      // show some loading message
      this.$el.html('Loading');

      // fetch, when that is done, replace 'Loading' with content
      this.collection.fetch().done(function(){
          var renderedContent = self.template(self.collection.toJSON());
          self.$el.html(renderedContent);
      });
      return this;
    }

  });
  // Our module now returns our view
  return MitarbeiterListView;
});

さらに別の方法は、フェッチ オプションに入れることができるコールバック関数を使用することを除いて、これと非常によく似たことを行うことです。success

于 2013-03-22T17:49:40.417 に答える
1

コレクション フェッチは、フェッチが完了すると「リセット」イベントをトリガーします。そのフェッチイベントを利用できます

this.collection.on('reset', this.render);
于 2013-03-22T17:47:24.043 に答える
1

fetch also allow you to pass a success handler function which will be called when fetch succeeds, i.e.:

  this.collection.fetch({success: this.render});
于 2013-03-22T18:52:52.247 に答える