Answer the question
In order to leave comments, you need to log in
How to send a POST from a form using Backbone?
Hello!
There is a problem with the following content -
There is a backend server written in the Flask framework.
There is an html-form from where you need to send data to the server (creating a new user).
The request must be sent using Backbone.
The save method doesn't help much. I would like to know what is the problem.
Perhaps I wrote the models incorrectly?
Modelka.
define(["underscore", "backbone", "jquery"],
function(_, Backbone, $) {
return Backbone.Model.extend({
urlRoot: "/adduser",
defaults: {
"email": "",
"f_name": "",
"id_role": 0,
"l_name": "",
"login": "",
"password": "",
"status": 1
}
});
});
define([
"underscore",
"backbone",
"jquery",
"text!pages/RestaurantPage/templates/AdminTemplate.html",
"pages/RestaurantPage/models/User"
],
function(_, Backbone, $, AdminTemplate, User) {
return Backbone.View.extend({
events: {
events: {
'submit #new_user': 'save'
}
},
initialize: function() {
this.collection = new User();
this.collection.on("change", this.collection.save());
},
el: $('#content'),
save: function(e) {
alert('ok"');
var userInfo = {
email: this.$('#email').val(),
l_name: this.$('#l_name').val(),
f_name: this.$('#f_name').val(),
login: this.$('#login').val(),
password: this.$('#password').val(),
id_role: this.$('#id_role').val(),
status: this.$('#status').val()
};
this.collection.save(userInfo);
},
render: function() {
this.$el.html(AdminTemplate);
}
});
});
Answer the question
In order to leave comments, you need to log in
The save method doesn't help much. I would like to know what is the problem.
this.collection.on("change", this.collection.save());
obviously redundant. Try like this:
define([
"underscore",
"backbone",
"jquery",
"text!pages/RestaurantPage/templates/AdminTemplate.html",
"pages/RestaurantPage/models/User"
],
function(_, Backbone, $, AdminTemplate, User) {
return Backbone.View.extend({
el: $('#content'),
events: {
'submit #new_user': 'save'
},
initialize: function() {
this.model = new User();
},
render: function() {
this.$el.html(AdminTemplate);
}
save: function(e) {
alert('ok"');
var userInfo = {};
_.each($('#new_user')[0], function(el) {
userInfo[el.id] = el.value;
});
this.model.save(userInfo,{
success: function(res){console.log('succes'),
error: function(res){console.dir(res)}
}});
},
});
});
define(["underscore", "backbone"],
function(_, Backbone) {
return Backbone.Model.extend({
url: function () {
return "/adduser";
},
defaults: {
"email": "",
"f_name": "",
"id_role": 0,
"l_name": "",
"login": "",
"password": "",
"status": 1
}
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question