I am using jQuery UI to allow the user to move a div. This would be the view side of things. Now the user moves the div to a new postition (x,y). I use jQuery to get the (x,y) value.
How do I "pass" the new (id,x,y) value to my node server(controller) so the server can save it to mongodb(model)?
The most straightforward way, it seems, would be to expose an API on your server to handle the new values.
For the routing part: Check out this article for how to create your own routing system. You could also just use Express or Connect to setup routes (which comes at the advantage of being for free). More info on which router to choose here: Which Node.js Router Should I Use?
For saving in the db
Assuming that you decide to go with Express, and that your API is : /user/:userId/view/:viewId/position, you could then do:
var mongoose = require('mongoose');
var View = new Schema({
id: String,
x: Number,
y: Number
});
mongoose.model("View", View);
And, in your router handler:
exports.post("/user/:userId/view/:viewId/position", function (req, res) {
//Find user with userId in db
//...
//Find view with viewId in db
var view = req.params.viewId;
View.findOne({id:view}, function(err, result){
if(err || !result){
console.log("cannot find view "+view);
res.end();
}
else {
//update value of x and y
var x = req.body.x;
var y = req.body.y;
//...
}
});
For sending the data Finally, on the client side, use jQuery to make the POST request:
$.ajax({
type: "POST",
url: window.location.href+"/view/"+viewId+"position",
data: {
x: 200,
y: 150
},
success: function(d) {
//Position saved
},
error: function(jqXHR, textStatus, errorThrown) {
//Something bad happened
}
});