开发者

Mapping javascript objects to their ids in an MySQL database

开发者 https://www.devze.com 2022-12-19 04:23 出处:网络
This is a question that I have been pondering for a long time, but didn\'t want to ask because I wasn\'t sure how to describe it.. I\'m still not sure if I can describe it well but here it goes..

This is a question that I have been pondering for a long time, but didn't want to ask because I wasn't sure how to describe it.. I'm still not sure if I can describe it well but here it goes..

I have a web app that allows you to manipulate a bunch of elements on the p开发者_如何学Goage, but has one save button. When I hit save I would like to create/update all of these changes in one POST (not incrementally). If these html elements were created for the first time on the page, I would like to insert them as new entries into the database. When these changes are saved in the database for the first time (created), I return the index ID, so if I make changes again, they will be updated in the database instead of created again.

What makes it tough is batching this save so it doesn't take up all this bandwidth. I want to be able to mix and match creates and updates, but sending back IDs from the created elements and mapping them to the correct html elements (so they will be updated next time) requires me to know something about the order of each batched element which leads to some issues. I was wondering if there is a clever way to do creates or updates, and map the IDs correctly back to various elements under one ajax request.

Hopefully this was clear, let me know if you need clarification

Thanks, Matt Mueller


You can just use negative auto-decremented ids for newly created elements and return a map from negative to positive ids. E.g., send [{id: 507, name: "foo"}, {id: -1, name: "bar"}, {id: -2, name: "baz"}], return {-1: 510, -2: 511}, and have your javascript update its ids based on the map.


well, upon loading, fetch next ID from db, so you know, what ID will have next page part in database. then, upon creation, in javascript you can work with this - i.e:

Next ID is 15, so when you add next field, it's ID is 16 and there is ID 17 in stack... and when you delete one newly created field, isn't problem to shift every higher ID one down.

Then, in your saving script, you know, that every ID larger than what you previously fetched is "to be saved" and every smaller is to be updated.


If your page knows when a new element is created, then it should arrange for the "id" parameter for that group of parameters (i.e., the attributes of the entity to be added) to be either null (not supplied) or some marker value. Already-existing entities have their ID value, which would not be changeable by the client. The server simply has to separate out the groups of parameters with empty ID values from those with non-empty values.

Your client may also want to mark elements for deletion. In that case, the already-existing entities would have their ID sent back with some flag parameter indicating "DELETE ME". Entities that are created and then deleted before "submit" would need no server processing.

0

精彩评论

暂无评论...
验证码 换一张
取 消