collection.reset

resetcollection.reset([models], [options])
Adding and removing models one at a time is all well and good, but sometimes you have so many models to change that you'd rather just update the collection in bulk. Use reset to replace a collection with a new list of models (or attribute hashes), triggering a single "reset" event on completion, and without triggering any add or remove events on any models. Returns the newly-set models. For convenience, within a "reset" event, the list of any previous models is available as options.previousModels.
Pass null for models to empty your Collection with options.

Here's an example using reset to bootstrap a collection during initial page load, in a Rails application:

<script>
  var accounts = new Backbone.Collection;
  accounts.reset(<%= @accounts.to_json %>);
</script>

Calling collection.reset() without passing any models as arguments will empty the entire collection.

doc_Backbone
2016-04-17 12:21:28
Comments
Leave a Comment

Please login to continue.