简体   繁体   English

使用jQuery和AJAX调用的最佳实践

[英]Best Practice with jQuery and AJAX calls

I have a question about best practices when using jQuery/JavaScript/Ajax. 我对使用jQuery / JavaScript / Ajax时的最佳实践有疑问。 Lets say that I have some tasks and there is a calendar for every task. 让我们说我有一些任务,每个任务都有一个日历。 The User is able to click on a day in a task calendar and book the task at the specific day via AJAX. 用户可以单击任务日历中的某一天,并通过AJAX在特定日期预订任务。 I have to store the date and the ID of the task somewhere and i am using really bizarre IDs for that such as: 我必须在某处存储任务的日期和ID,我正在使用非常奇怪的ID,例如:

<span class="day_field" id="date_13-02-2013_task_4">13.02.2013</span>

Then i just attach an listener like this: 然后我只是附上一个这样的听众:

$('.day_field').on('click',function(){
    var date = $(this).id.split('_')[1];
    var task_id = $(this).id.split('_')[3];
    //place for some validation
    $.post('book_task.php',{task_id: task_id, date: date},function(data){
        //something cool with the result
    });
});

My question is: Is this the right way how to do it? 我的问题是:这是正确的方法吗? I am not pretty sure, because the IDs can be really long + it contains ID in database which is not probably save at all. 我不太确定,因为ID可能非常长+它在数据库中包含ID,这根本不可能保存。

Thanks! 谢谢! T. T.

Use HTML5 data attributes: 使用HTML5数据属性:

<span class="day_field" data-date="13-02-2013" data-task="4">13.02.2013</span>

$('.day_field').on('click',function(){
    var date = $(this).data("date");
    var task_id = $(this).data("task");
    //place for some validation
    $.post('book_task.php',{task_id: task_id, date: date},function(data){
        //something cool with the result
    });
});

The right way A better way to do it would be to store the data in either data attributes, or make the span an anchor tag and store the param string desired in the href attribute. 正确的方法 更好的方法是将数据存储在数据属性中,或者将span作为锚标记并存储href属性中所需的参数字符串。

<span class="day_field" data-date="13-02-2013" data-task="4>13.02.2013</span>

or

<a class="day-field" href="?task_id=4&date=13-02-2013">13.02.2013</a>

with this for the anchor tag: 用于锚标记:

$('.day_field').on('click',function(e){
    e.preventDefault();
    $.post("foo.php",this.href,handler);
});

Instead of an ID, you can use custom data attributes , like this: 您可以使用自定义数据属性 ,而不是ID,如下所示:

<span class="day_field" data-date="date_13-02-2013_task_4">13.02.2013</span>

And then you can access the value like this in jQuery: 然后你可以在jQuery中访问这样的值:

$(".day_field").data("date");

Exposing the actual ID of something in your database is only as insecure as your database. 在数据库中公开某些内容的实际ID仅与数据库一样不安全。

Using the id of the element seems fine to me, too, if it uniquely identifies a thing. 如果它唯一地标识了一个东西,那么使用元素的id似乎也很好。 Using the data attributes is a possibility to save on splitting logic if you like, but you could still use id in tandem. 如果您愿意,可以使用data属性保存分裂逻辑,但您仍然可以串联使用id

Conventionally speaking, this is very tame code compared to much of what jQuery is. 从传统上讲,与jQuery的大部分内容相比,这是非常温和的代码。

One more elegant way to associate data to an element is to use jQuery's data . 将数据与元素相关联的另一种更优雅的方法是使用jQuery的数据 However, I would consider building a jQuery plugin and using one instance of it for each task. 但是,我会考虑构建一个jQuery插件,并为每个任务使用它的一个实例。 A plugin encapsulates all of the data it needs, so you wouldn't need to store it tied to the element, which is not great. 插件封装了它需要的所有数据,因此您不需要将它存储在与元素绑定的位置,这不是很好。

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

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