Backbone.js: the best way to save the changed model identifier on the server

I want to allow the user to change the model id in the collection.

Then I have a problem that when calling model.save() model is saved in the wrong place. This is put for the URL of the new id instead of the URL of the old id

 var model = collection.get('oldid'); model.set({id: 'newId'}); // set by the user through the ui model.save(); 

This will persist until collectionUrl/ + 'newId' instead of collectionUrl/ + 'oldId'

I want to save (put) {id: 'newId'} to the old URL.

What is the most consistent way to do this?

+4
source share
4 answers

This is such an extreme case that for him there are no best practices.

I would try rewriting Backbone.sync and / or Backbone.Model save to make the spine behave this way. I did a similar thing when I use the trunk in the Air application while saving directly to the local database.

- change

Or maybe Backbone.Model url .

+4
source

This can also be achieved by setting the wait parameter to true for the model save method. This prevents the model URL from changing until the server returns an updated view with a new identifier. I tried this and it seems to work great!

+3
source

I do not think that any framework, server side or client side would support this out of the box. The best way would be to make your own AJAX request to your own endpoint on the server. I would question why you need to do this, although I can't think of any reasons that would be a good idea.

+2
source

this is a little circular movement, but you can create a separate unique identifier (specified using the idAttribute model identifier), then "id" is just another property.

Most (all ...?) Frameworks assume that the identifier is unique and immutable.

Just be aware that changing an identifier has so many consequences. Any links related to other objects should now be damaged, URLs that link to the previous identifier will be broken, etc.

0
source

All Articles