Closed gilbert closed 12 years ago
Backbone.offline generated new ids for every new object which was added to storage. localStorage is like local DB. When you offline you can not generate id on server and you using local-id for consistence. You still can use Collection#get
and it will be always correct no matter was synced this model or not. And feature with duplicate id controlling in the phase of sync. I agree that we should delegate it to Backbone, but now it's still not so.
Why I don't use cid:
Cid
generated backbone and it's different between user sessions. You don't know which Backbone objects you will be use in current session. Although your idea is interesting and I should thinking about it and this way make library more flexible and more Backbone oriented.
Your way is not typical use case. You want to use backbone.offline as a temporary cache. That's mean that you should use sid
for relations between other collections. But when you don't have a sid
what id
do you want to save to relational model?
Thanks for the explanation. Since the app will be available offline, backbone.offline does fit my use case since I don't want to rely on the server to create tags.
I think I just need a way to tell backbone.offline "Here is a model from the server". Something like an update()
method, or an option for pull()
. This method/option would assume that the given model/data is already saved on the server, so it would copy over the id to sid and check for duplicates based on that.
Yes, an option to pull()
does sounds like a good idea. It would use the data you give it instead of getting it from the server. Thoughts?
I think we should use option in init mode like localId: false
. It will work good when you don't want to relate your collection with others.
Bad offline case:
In offline mode you created a model and was generated id: sdhfshdkfj-sdfds-sdfsd-sdf
. You connected this model with other models and saved primary ids. When you will sync this model which id will you use? We shouldn't change primary id on sync, it involves bad consequences.
Does your bad offline case work with backbone.offline now? The option I'm suggesting would only be used when you receive data from the server from somewhere besides pull()
. In other words, it will already have a proper id; backbone.offline would treat it as if it did come from pull()
, changing the id and sid as it normally would.
After studying the source code, it turns out there's already a method for this: Offline.Sync#pullItem
. This fits what I need.
I'm going to be studying backbone.offline more extensively, since my app needs to be fully useable offline. Just fyi
Ok. Glad to hear that you decided your problem and look forward for new experience and probably pull requests. Good luck!
So it looks like you copy
id
intosid
and generate a new guid forid
. Why is this necessary? I'm asking because I'm not familiar enough with Backbone's source to know.Changing
id
loses some useful backbone properties:Collection#get
method retrieves a model by its idsid
, which might not exist for newly created modelsIt's possible I'm taking the wrong approach. Here is my use case:
create
methodCan you explain why you're modifying
id
, and not using a client id similar to Backbone's cid? I want to understand so I can submit a patch if needed.