Updating the model by form in the highway

I am creating a simple application that uses Backbone and Laravel to manage orders, and I am working on a simple form to update user data for the current user.

I was wondering if there is a better, more efficient way than the one I did to update your model with the input from the form?

I created a method called updating in the model to which the form's DOM object is passed. I guess this is not the best way to do this.

Any help would be greatly appreciated!

var Account = Backbone.Model.extend({

    url: "/settings/account",

    initialize: function()
    {

    },

    update: function(form)
    {
        this.set({
            first_name : form.find('#first-name').val(),
            last_name : form.find('#last-name').val(),
            email : form.find('#email').val(),
            landline: form.find('#landline').val(),
            mobile: form.find('#mobile').val()
        });

        return this.save();
    }

});

var user = new Account;

var AccountView = Backbone.View.extend({

    el: $("div.section"),

    template: _.template($("#account-template").html()),

    events: {
        "submit #account": "update"
    },

    initialize: function()
    {
        _.bindAll(this, 'render', 'update');
        this.render();
    },

    render: function()
    {
        $(this.el).html(this.template(this.model.toJSON()));
    },

    update: function()
    {
        var form = $(this.el).find('form#account');

        user.update(form);

        $(this.el).find('.alert-success').show();

        return false;
    }

});

var Router = Backbone.Router.extend({

    routes: {
        "account": "account"
    },

    account: function()
    {
        user.fetch({
            success: function()
            {
                return new AccountView({model:user});
            }
        });         
    }

});
+5
source share
1 answer

This is considered poor design when the model is aware of its presentation. I would update the model organized by the view.

, :

update: function()
{
    var form = $(this.el).find('form#account');
    user.set({
        first_name : form.find('#first-name').val(),
        last_name : form.find('#last-name').val(),
        email : form.find('#email').val(),
        landline: form.find('#landline').val(),
        mobile: form.find('#mobile').val()
    });

    user.save();
    $(this.el).find('.alert-success').show();
    return false;
}

, . , - , .

+6

All Articles