AmpersandJS / ampersand-rest-collection

ampersand-collection with REST and lodash mixins for easy use with REST APIs.
http://ampersandjs.com/docs#ampersand-rest-collection
MIT License
23 stars 11 forks source link

Sync entire collection with server-side #17

Closed ivan-kleshnin closed 9 years ago

ivan-kleshnin commented 9 years ago

I don't understand how it should be implemented. Manually (iterate and model.save / model.destroy one by one) or via some existing method?

There are something called "sync"

model.sync(method, collection, [options])

which is entirely undocumented and I had no luck trying to run this. Even its signature looks broken (this is a quote from "ampersand-rest-collection" doc link so it ought to be collection.sync(...) at least!)

I thoroughly reviewed to this moment:

Someone states that this is low-level method and it should not be used for basic tasks. However the question remains. Basically I expected to empty entire collection on client and remove corresponding data on server by issuing next commands:

collection.reset();
collection.sync();

Technically, resulting error from the second line is

A "url" property or function must be specified

but this is obviously due to wrong arguments. By Backbone docs it looks like it should be called like

collection.sync("delete", collection, {})

which is totally bizarre. So I surrender and ask for help.

kamilogorek commented 9 years ago

From what I understand you want to completely clear your collection on the client-side and get up to date data from the server?

var Photos = AmpersandRestCollection.extend({ url: '/photos' });
var photos = new PhotosCollection();

photos.add([{ ... })]; // let's say you've added something, but you want to get rid of it and fetch data from server

photos.fetch({
    reset: true // <= this will reset entire collection before putting your data in it
});

Is that what you wanted to achieve?

ivan-kleshnin commented 9 years ago

No, I want to clear data on client and (as a consequence) remove it on server. In conventional way.

aaronmccall commented 9 years ago

From what I'm seeing, there is no built-in way to do a batch delete. You'd need implement that in your collection definition. The sync method is a low-level method that is used by fetch, getOrFetch, and fetchById. If called directly, it should be called with request type (one of: create, read, update, delete, patch), the collection, and options object (optional).

I speculate that that batch delete has been left out since how to do it is highly dependent on what the server-side makes available for that purpose.

ivan-kleshnin commented 9 years ago

Thank you, I'll give it a second look.