douzhuoxia0587 2010-02-06 23:11
浏览 66
已采纳

将javascript对象映射到MySQL数据库中的id

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

  • 写回答

3条回答 默认 最新

  • dousi7919 2010-02-06 23:21
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致