backbone ajax 模板,backbone.js - Make AJAX call from Handlebars template? - Stack Overflow

作者正在构建一个基于Mongoose、Node.js、Backbone和Handlebars的SPA,遇到问题:如何从Handlebars模板中获取用户选择的课程ID并传递给外部AJAX删除请求。文章将展示如何解决在模板间传递变量到AJAX请求的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am building an SPA on Mongoose, Node, Backbone stack and using Handlebars. A user signs up for one or more courses. Then, user can go to his list of courses and cancel if he chooses. The API works, I can remove the courses from the database making an ajax call to the specific url from the console. The problem is I need the ajax call to take the variable from the handlebars template. How do I capture the {{_id}} from my handlebars template and pass it into an AJAX delete request that is outside the template?

template: Handlebars.compile(

'

{{local.petname}} is a busy beast!
'+

'

Upcoming Courses:

' +

'

  • '+

'{{#each signup}}' +

'

{{name}}

{{coursDay}}{{time}}{{location}}' +

'Remove Course
' +

'

{{_id}}'+

'{{/each}}'+

'

'

),

Here is my ajax request; attrID returns the ObjectID of only the first item in the array

deleteItem: function(event) {

event.preventDefault();

var attrID = $('.btn-danger').data();

console.log(attrID);

jQuery.ajax({

url: "/test/signups/" + attrID,

type: "DELETE",

success: function (data, textStatus, jqXHR) {

console.log("Post response:");

console.dir(data);

console.log(textStatus);

console.dir(jqXHR);

}

});

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值