I'm currently working on passing data from the client to the server using Backbone.js's save() method.
task_manager.js
window.Task = Backbone.Model.extend({
initialize:function(){
},
urlRoot:"http://localhost:3000/api/tasks",
defaults:{
task:"",
completed:false,
category:"",
priority:""
},
toggleCompletion:function(){
this.destroy();
}
});
server.js(Node/Express)
app.post("/api/tasks",function(req,res){
console.log(req.task); // I am expecting to receive the task item's title of a newly created item, but req.title is undefined.
});
app_view.js(Backbone.js) implementation is shown below...
...
// The create method is supposed to create a new task item and send it to the server. However, it is not working as expected.
create:function(e){
var input = $(this.el).find("#new-task");
if (e.keyCode !== 13) return;
var text = input.val();
var task = new Task({task:text});
// This should send a POST request to "/api/tasks"
task.save({task:text,body:"hello world"});
var taskView = new TaskView({model: task});
$("#task-item").append(taskView.render().$el);
$(input).val("");
this.collection.add(task);
}
});
This code snippet is failing to pass data from the client to the server. Do you have any suggestions or ideas? Thanks in advance.