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

被折叠的 条评论
为什么被折叠?



