Backbone.js calling render after collection is populated

后端 未结 4 1236
情书的邮戳
情书的邮戳 2021-01-28 14:49

I\'m trying to call render after fetch of a collection.

In my initialize method I have:

this.collection.bind(\'all\', this.render, this);
this.collection         


        
4条回答
  •  半阙折子戏
    2021-01-28 15:42

    If you want to guarantee that data has been fetched before rendering it, then I'd suggest using jQuery's deferred object:

    this.collection.deferred = this.collection.fetch();
    self = this;
    this.collection.deferred.done(function() {
        self.collection.render();
      }
    );
    

    Basically anything you put into the function you send to done will only be called after fetch is, well, done.

    More on deferreds:

    • http://lostechies.com/derickbailey/2012/02/07/rewriting-my-guaranteed-callbacks-code-with-jquery-deferred/
    • http://www.erichynds.com/jquery/using-deferreds-in-jquery/

提交回复
热议问题