Update
Demo: http://jsbin.com/ogorab/311/edit
I'm trying to build a simple chat room that updates in realtime using Faye/Websockets. Messages are posted using regular REST, but there is also a subscription via Faye to /messages/created, which uses store.pushPayload
to push the new messages.
Now the following scenario happens and I can see where it goes wrong but I have no clue how to solve it:
ChatController
handles the submit, calls createRecord
with the chat message, and subsequently #save
createRecord
). Note that no id
has been assigned yet.This results in duplicate messages, in the following order:
createRecord
, will resolve via ajax response]pushPayload
/Faye]I hope you can understand so far. A solution would be to have Faye wait for the save call to resolve before pushing the payload. Unfortunately I don't have a reference to the record that is being saved (happens in a controller, faye subscription is set up in ApplicationRouter
).
Also I would like this to work in a generic way:)
Finally found a solution for this, but other suggestions are still welcome.
Turns out that Store#didSaveRecord
updates the id after the record is saved. By overriding this method (and then calling super, in that order), we can first check if a record for that id already exists:
App.Store = DS.Store.extend
didSaveRecord: (record, data) ->
# This will remove any existing records with the same id
@getById(record.constructor, data.id)?.unloadRecord()
@_super(record, data)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With