Data: Allow Store#pushPayload to delete records in store

Created on 15 May 2014  路  11Comments  路  Source: emberjs/data

It would be nice to allow the deletion of records via a specially formatted message passed to Store#pushPayload. This would allow api designers to send a message for a record indicating it was deleted.

The use case for this for me at the moment is for integration with WebSockets. I'm building an adapter which calls pushPayload when it receives a message.

var _this = this;
var socket = new WebSocket('ws://' + window.location.host + '/api/v1/events/doors');
socket.onmessage = function(event) {
  _this.get('store').pushPayload('door', JSON.parse(event.data));
}

Maybe sending a message like

{
  "post": { "id": "1" },
  "_method": "delete"
}

Allowing this to also remove records would be awesome.

Most helpful comment

This is how tickets get lost.

All 11 comments

We've run into something similar on a current project. We're using an EventSource to push data to a client and we include a flag similar to the _method you've suggested. We just branch based on the type of method. If it's 'add' we pass the payload along to pushPayload. If it's delete we'll call deleteRecord with the id in the payload. To me it seems like in the more general case you could do the same sort of logic in the adapter rather than keeping it in pushPayload.

Yea, I'm implementing something like that with Websockets. Seems like a very common pattern, might be nice to support it.

@wycats any thoughts?

We do something similar but include a type along with our payload.

  {
    "id": "123",
    "type": "post",
    "method": "create",
    "data": {}
  }

or

  {
    "id": "123",
    "type": "post",
    "method": "delete"
  }

So in the case of create/update we can use pushPayload and delete we can run a deleteRecord on it. I could definitely see the benefit of just using pushPayload and avoid some of that code we have. I assume syncing front and backend data is a somewhat common practice.

I think this should definitely be supported by ember-data, but it might be a good idea to go through the process of RFC so the specific API can be fleshed out.

Closing this in favor of being discussed further at https://github.com/emberjs/rfcs

This is how tickets get lost.

@nixpulvis you're right! Reopening and will close once such a PR exists.

This would indeed be very useful.
+1 from me!

Would something like this need to make it's way in to JSON API first?

+1, I could use this too. Although I'm still stuck on the ActiveModelAdapter so it would be nice if it has across-the board support.

@Asherlc, yep. Having such a functionality defined in JSON-API at first would definitely help this issue being moved forward.

https://github.com/json-api/json-api/issues/795#issuecomment-240385923 is a relevant comment.

Was this page helpful?
0 / 5 - 0 ratings