Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember data - find out if a record has been saved to the server

Tags:

ember-data

I'm running into a situation where I have an ember-data record and I don't know if it has already been saved to the server or not. Is there any way to find out?

(Using ember-data (1.0.0-beta.11) and ember (1.8.0))

Before saving to the server the ID will be something like 9gdh5 so I was initially just checking isNaN(+record.get('id')) but unfortunately this fails occasionally when the temporary ID generated by ember doesn't contain any letters.

Searching revealed that ember-data previously used a _clientId property but this seems to have gone. Am I missing something obvious or do I need to manually manage a flag saying whether a record came from the server or was generated locally?

Here is some information about my use-case in case it helps to clarify the question...

My app works both on and offline. My custom adapter knows when it is offline and queues requests to LocalStorage to send when online. The user can update records and when the adapter receives this update it needs to know if the record has been synced to the server yet. If so it queues an PUT request to be sent to the server when online. If the record hasn't been synced yet we can't do a PUT request because we don't know the record's ID (which is part of the URL for the PUT). So instead we have to find the locally stored POST request and update that instead. So I want a reliable way to find out if a record has been synced to the server or not...

like image 984
vitch Avatar asked Oct 31 '22 13:10

vitch


1 Answers

http://emberjs.com/api/data/classes/DS.Model.html#property_isNew

This might be new since this issue was created but I came here looking for the answer so thought I would paste what I found.

like image 61
Robbo Avatar answered Dec 20 '22 01:12

Robbo