简体   繁体   English

关于识别用户想要编辑/删除的元素的建议

[英]Suggestion on identifying elements that user wants to edit/delete

I started learning Javascript a month ago, and I am trying to build projects by myself.一个月前开始学习Javascript,正在尝试自己搭建项目。

To this point I have been using data attribute in html to pass an unique ID and based on that ID I edited/deleted element/item off the list.到目前为止,我一直在使用 html 中的数据属性来传递唯一 ID,并根据该 ID 从列表中编辑/删除元素/项目。

For example I am making a Workout Track app and when user enters data, list item is created and inserted with unique ID.例如,我正在制作一个 Workout Track 应用程序,当用户输入数据时,将创建列表项并使用唯一 ID 插入。 For example:例如:

 <li class="workout workout--${workout.type}" data-id="${workout.id}">

Afterward I listen for click on entier List Container and based on proximity to the list item and clicked button(Edit/Delete) I get the ID and find object in the array.之后,我监听了对 entier 列表容器的点击,并根据与列表项的接近度和点击按钮(编辑/删除)我得到了 ID 并在数组中找到了 object。

const workout = this.#workouts.find(work => work.id === workoutEl.dataset.id);

This is an easy system and it works until you mess with ID in HTML. So what would be a better and safer way to implement what I need.这是一个简单的系统,在您弄乱 HTML 中的 ID 之前,它一直有效。那么什么是实现我需要的更好、更安全的方法。 I still check if ID exists and if it belongs to user to process it further, but is there any other smarter way of doing this.我仍然检查 ID 是否存在以及它是否属于用户以进一步处理它,但是还有其他更聪明的方法可以做到这一点。

TLDR: Smarter/better way of handling CRUD operations based on ID of element/item, besides using HTML data TLDR:除了使用 HTML 数据之外,基于元素/项目的 ID 处理 CRUD 操作的更智能/更好的方法

If the client messes with the HTML manually, all bets are off.如果客户端手动弄乱了 HTML,则所有赌注都将取消。 There's no way to fully guard against such things - ultimately, it's the client's machine, and the client can run whatever code they want on it.没有办法完全防范这样的事情——归根结底,它是客户的机器,客户可以在上面运行他们想要的任何代码。 Trying to prevent that is a waste of time.试图阻止这种情况是浪费时间。 Rather, if the client wants to do something that affects something outside of their browser (such as make a change to the database that the data is sourced from), you have the right idea - validate it on the server to make sure they have permissions to do what they're trying to do.相反,如果客户端想要做一些影响浏览器之外的事情(例如更改数据来源的数据库),那么您的想法是正确的 - 在服务器上验证它以确保他们具有权限做他们想做的事。

That's honestly all you need to do, and is very common practice.老实说,这就是您需要做的所有事情,并且是非常普遍的做法。

You can make it more difficult for the user to mess with the logic of the application by putting the IDs in, say, a WeakMap that links a <li> to a particular workout:您可以通过将 ID 放入将<li>链接到特定锻炼的 WeakMap 中,让用户更难扰乱应用程序的逻辑:

const workoutsByLis = new WeakMap();

// ...

// when you create a LI:
const workoutLi = document.createElement('li');
workoutLi.className = `workout workout--${workout.type}`;
workoutsByLis.set(workoutLi, id);

This way, the ID isn't exposed in the HTML or retrieved from it.这样,ID 就不会暴露在 HTML 中,也不会从中检索到。 To get the ID from a clicked <li> , you'd then just do要从点击的<li>中获取 ID,您只需执行

const workoutId = workoutsByLis.get(e.target);

(or e.currentTarget - however the listener is attached, navigate to the <li> , then pass it to workoutsByLis.get ) (或e.currentTarget - 无论侦听器是否附加,导航到<li> ,然后将其传递给workoutsByLis.get

Or you could not bother and keep using your current approach, which is fine.或者您不会费心继续使用当前的方法,这很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM