简体   繁体   中英

How can I set my own id on each model using Backbones collection parse method?

I'm fetching data that currently comes back to me with no id and is causing issues in my datastore, it looks like the id is actually buried in the JSON so ideally what I would like to do is create an unique id for each model in the collection but I'm not entirely sure how to do this, can anyone help explain how this may be done? I know I need to use the parse method but I'm not sure how I loop through the fetched data or maybe this gets done in the model? I'm not sure what code I need to show you here either.

In my opinion it is a little 'wrong' if you're retrieving data from server. The ideal is the server return these id's for you.

But to do that is something like this:

parse:(ObjectReturnedFromServer)->
    id = // Get an ID from somewhere
    @set('Id', ObjectReturnedFromServer)

Once again, it makes no sense to me, but will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM