jo / backbone-pouch

Backbone PouchDB Adapter
http://jo.github.io/backbone-pouch/
MIT License
163 stars 27 forks source link

.save() removes attributes from model ? #36

Open peritus opened 9 years ago

peritus commented 9 years ago

Hey. Thanks for backbone-pouch, everything is very smooth so far :)

I'm porting an application that saves the models on a remote server via Backbone's XHR to PouchDB and backbone-pouchdb. In the current version, save() sends the complete JSON representation to the server and retrieves a complete JSON representation back from the server (I guess that's custom to my application, but I believe that is a nice REST architecture and allows the server to also change the document).

So, in Model.parse, my code (quoting http://backbonejs.org/#changelog 's 1.1.0 section here)

"extract[s] and vivify[s] incoming nested JSON into associated submodels. "

With backbone-pouchdb, however, the data argument for parse is always an object containing just _id and _rev, without the attributes that have just been saved, because of these lines:

response = {
  _id: response.id,
  _rev: response.rev
};

I'd propose to respond with this instead:

response = extend({}, model.toJSON(), {
  _id: response.id,
  _rev: response.rev
});

so the model doesn't think the the server (or PouchDB in this case) deleted all the attributes.

Also, one could argue to only call model.toJSON() once, but I wanted to discuss in general this first before making optimizations.

jo commented 9 years ago

CouchDB, and so PouchDB, always responds with id and rev on successful document updates:

{
    "id": "SpaghettiWithMeatballs",
    "ok": true,
    "rev": "2-790895a73b63fb91dd863388398483dd"
}

I suggest to rewrite the parse functions in a way that they are non-destructive.

jo commented 9 years ago

Please reopen if still an issue