Backbone collection.create() does not return the updated model

前端 未结 1 1890
挽巷
挽巷 2021-01-05 19:14

To learn backbone Im creating a Twitter like app. So you know that Twitter sends a GET request to the server every N seconds to check for new tweets. If there are new tweets

相关标签:
1条回答
  • 2021-01-05 20:03

    create is still asynchronous even if you pass { wait: true }. The difference is without wait the model will get added to the collection immediately while with wait backbone won't add it to the collection until a success response from the server.

    What you should do is add a success callback to create that fires the event upon the server response.

    var created_tweet = this.collection.create( newTweet, { silent: true, wait: true, success: this.successCallback } );
    
    // Add successCallback as a method to the collection
    successCallback: function(collection, response) {
      // I'm not 100% positive which values are passed to this function. Collection may actually be the new model.
      this.collection.trigger("posted_new_tweet", created_tweet);
    }
    
    0 讨论(0)
提交回复
热议问题