views:

49

answers:

3

Hey all,

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 page, 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

+1  A: 

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.

bluej100
That is ingenious! So for elements you just need to update, would you return from the server something like "{40:40}"?
Matt
Aye. I mean, it can just assume that elements not in the map keep their same ids, but it certainly works either way. I'm glad you like it. :)
bluej100
Yah good call. That's how I implemented it. I never think of using negative numbers as an alternate state! So elegant..
Matt
+1  A: 

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.

Adam Kiss
Well, what happens if two people are using the page? You can't assume that new values will be those whose keys are beyond what used to be there.
Pointy
the - saving goes everytime in ONE moment... so when you are about to save and know everything beyond 15 is new - you simple fetch from DB new newID (say 20), edit new IDs and save it to DB - no harm done.
Adam Kiss
This would work under normal circumstances! I didn't explain my application well enough for you to know any better but basically everything is "created" on page load. Its an ajax app, so during the life of the interaction things can be updated and created but at the beginning everything starts off created. When I add "edit" functionality this would work perfect. Thanks!
Matt
+1  A: 

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.

Pointy
Yah, exactly. that's what I do. It still doesn't get past the fact that inside this create group, I need to know how they were batched (in which order were the creates made). Check out @bluej100's response. I think this is an effective way to solve this problem. Thanks for your help!
Matt