S
S
semolex2014-04-23 01:08:31
backbone.js
semolex, 2014-04-23 01:08:31

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
            }
        });
    });

view:
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);
            }
        });
    });

Form ID - new_user...
I would be grateful for your help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Kitmanov, 2014-04-23
@k12th

The save method doesn't help much. I would like to know what is the problem.

We would also like to know what the problem is :) What exactly does not work out?
Line
this.collection.on("change", this.collection.save());
obviously redundant.
Why are you putting the model in this.collection? My brain almost exploded.

P
personaljs, 2014-04-23
@personaljs

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 question

Ask a Question

731 491 924 answers to any question