BackboneJS collection.reset() vs collection.fetch()

后端 未结 3 871
北荒
北荒 2021-02-04 05:41

I have read and read the docs on these two methods, but for the life of me cannot work out why you might use one over the other?

Could someone just give me a basic code

3条回答
  •  醉话见心
    2021-02-04 06:11

    reset sets the collection with an array of models that you specify:

    collection.reset( [ { name: "model1" }, { name: "model2" } ] );
    

    fetch retrieves the collection data from the server, using the URL you've specified for the collection.

    collection.fetch( { url: someUrl, success: function(collection) {
        // collection has values from someUrl
    } } );
    

    Here's a Fiddle illustrating the difference.

提交回复
热议问题