javascript - 用户修改一个列表型的数据,后台和数据库互动时应该怎么操作好?
比如每个用户有一个自己的收藏列表,用户可以增加和删除列表中的任一项。
那么用HTTP请求和数据库互动时,以下哪种是最科学的呢?
1,每次增删都发起一次HTTP请求,在数据库上作相应操作(这个HTTP请求太多我自己觉得不科学)
2,把数据库上的列表数据下载下来呈现给用户后,只在前端的数据模型中增删,等到用户可能要关闭页面时用一次HTTP请求来同步,使数据库上的数据更新。不过我主要想问的是这里又有两种具体的互动方法,请大家赐教:
2a,在前端数据模型操作时,记录下哪些被增加了哪些被删除了。在更新数据库做Insert into... where value in和Delete ... where value in 这两次操作
2b,前端只记录数据模型。在更新数据库时直接truncate原来的表,把新的表数据插入。
请问2a和2b哪种比较好?
另外,第2步中同步的时机一般应该是什么时候呢?是用interval来定时同步呢?还是在用户进行一个特定的操作后来同步?谢谢。
-------1218更新
不好意思,上面忘记说了,我指的是用angularJS这种情况下,可以用数据模型来暂存数据的
回复内容:
比如每个用户有一个自己的收藏列表,用户可以增加和删除列表中的任一项。
那么用HTTP请求和数据库互动时,以下哪种是最科学的呢?
1,每次增删都发起一次HTTP请求,在数据库上作相应操作(这个HTTP请求太多我自己觉得不科学)
2,把数据库上的列表数据下载下来呈现给用户后,只在前端的数据模型中增删,等到用户可能要关闭页面时用一次HTTP请求来同步,使数据库上的数据更新。不过我主要想问的是这里又有两种具体的互动方法,请大家赐教:
2a,在前端数据模型操作时,记录下哪些被增加了哪些被删除了。在更新数据库做Insert into... where value in和Delete ... where value in 这两次操作
2b,前端只记录数据模型。在更新数据库时直接truncate原来的表,把新的表数据插入。
请问2a和2b哪种比较好?
另外,第2步中同步的时机一般应该是什么时候呢?是用interval来定时同步呢?还是在用户进行一个特定的操作后来同步?谢谢。
-------1218更新
不好意思,上面忘记说了,我指的是用angularJS这种情况下,可以用数据模型来暂存数据的
1.你为什么认为1是不科学的呢?天知道客户端哪边会发生什么事情~~当用户添加了N个收藏,猫猫过来把电源搞掉了-关机了,这个咋整哦~~~
2.用户希望它的每一步操作都是有效而真实的~~
3.删除动作,不要DELETE,UPDATE原纪录,做逻辑删除
4.truncate表这么暴力的招都用上啦,不要啊~~~~
只能每次发请求这是http
肯定要每次发送请求啊,你在前端怎么保存数据?如果不将用户的操作存入数据库,当用户不小心刷新页面或者关闭页面,他的操作在前端根本没有办法记录下来。
这种就看你要怎么设计了,如果你列表下面有个提交或者保存按钮,用户就明白N种操作完成必须提交或保存才能生效;如果你没有这种提示性信号,只是在每个列表后有个删除,然后有个随意增加的,用户理解成操作即保存,那就不能缓存处理了呗,只能实时性操作数据库
truncate这个有点不太科学,除非每个用户都有一个自已的收藏表
可以找找 window.onbeforeunload
这个事件 在事件中先判断用户收藏的数据是否有改动,有则http请求处理
最近我也在烦这个http请求次数的问题,看了你的描述之后,突然有个想法。。。但不知道可行不可行。。。
首先是获取数据的时候将数据存入一个数据模型里,然后让页面从这个数据模型里面拿取数据。
当页面发生修改事件时,将修改后的数据存入数据模型,然后再从模型拿出修改页面
然后开个定时器,隔一定时间就判断数据模型里面的数据是否发生改动,是就发生请求,不是就不发送。
最后当用户离开该页面的时候最后再判断一次。。。
。。。
或许可以当第一个修改事件发生的时候开启定时器。。。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
