I’m trying to pass the data from client to server via Backbone.js’s save() method.
todo.js
window.Todo = Backbone.Model.extend({
initialize:function(){
},
urlRoot:"http://localhost:3000/api/todos",
defaults:{
title:"",
done:false,
important:false,
completed:false
},
toggleDone:function(){
this.destroy();
}
});
app.js(Express)
app.post("/api/todos",function(req,res){
console.log(req.title); // I want to receive todo item's title which is created new item's.But req.title is undefined.
});
app_view.js(Backbone.js) is like this…
...
// create method make new todoItem and send new todoItem to server.But currently it doesn't work.
create:function(e){
var input = $(this.el).find("#new-todo");
if (e.keyCode !== 13) return;
var text = input.val();
var todo = new Todo({title:text});
// It send POST request to "/api/todos"
todo.save({title: text,body: "hello world"});
var todoView = new TodoView({model: todo});
$("#todo-item").append(todoView.render().$el);
$(input).val("");
this.collection.add(todo);
}
});
This code doesn’t send data pass from client to server.
Do you have any idea? Thanks in advance.
Your model data won’t be directly on the request object in Express (req.title).
Assuming you’ve configured express to use the bodyParser() to parse HTTP POST Bodies
Then your Backbone model will be in request.data: