如何在Backbone的输入字段中保存数据?

时间:2014-04-22 16:52:15

标签: backbone.js

我有问题 - 我需要将数据从表单放入数据库...使用Backbone使用! 怎么做? 我需要在我的文章和HTML中写什么? 我的收藏是:

define([
        "underscore",
        "backbone",
        "jquery"

    ],
    function(_, Backbone, $) {
        return Backbone.Collection.extend({

            url: '/users/all' //route for get ALL users in DB
        });
    });

1 个答案:

答案 0 :(得分:0)

每次用户点击提交时,您只需创建一个迭代html表单的方法:

store: function(e)
{
    e.preventDefault();

    //select each input, select and textarea within this views $el
    _.each(this.$('input, select, textarea'), function(input)
    {
        //you input name attribute to set the correct model attribute.
        this.model.set(input.name, input.value);
    }, this);

    //new model attributes from the form are now available and ready to be synced
    //with the server.
    console.log(this.model.toJSON());
}

选中JsFiddle Demo