Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 主干-我的模型总是保留一个id,并进行put而不是post_Backbone.js - Fatal编程技术网

Backbone.js 主干-我的模型总是保留一个id,并进行put而不是post

Backbone.js 主干-我的模型总是保留一个id,并进行put而不是post,backbone.js,Backbone.js,很抱歉,我的标题与此无关。我会在这篇文章中更好地解释:) 我理解我的问题,我找到了解决办法,但我不认为这是一个好办法,所以我想得到一些建议 我的工作流程: test: function(model) { model.on("invalid",this.showErrors, this); model.on('request',this.ajaxStart, this); model.on('sync',this.ajaxCo

很抱歉,我的标题与此无关。我会在这篇文章中更好地解释:) 我理解我的问题,我找到了解决办法,但我不认为这是一个好办法,所以我想得到一些建议

我的工作流程:

test: function(model) {
            model.on("invalid",this.showErrors, this);
            model.on('request',this.ajaxStart, this);
            model.on('sync',this.ajaxComplete, this);
        },
register: function(e) {
            this.model = new User();
            this.test(this.model);
            e.preventDefault();
   etc.
 }
  • 我有一个表单,用户可以在其中输入id
  • 我做了一个验证(空字段等),并在他单击submit时调用API
  • 如果id存在,我将在数据库中注册所有信息,并使用用户对象(Symfony2+fosRestBunble)发送响应
我的问题: 当我第一次点击表单时,它运行良好,一个新用户创建了它。但是,当我尝试注册第二个用户时,他会发出一个PUT请求,因为id是用前一个用户对象发送的

当我看到我的代码时,我理解为什么,因为我在视图的initialize函数中初始化了我的用户模型。(在外面之前)

以下是我的看法:

define(['backbone',
    'views/notification/form',
    'models/form',
    'text!templates/user-form.tpl'],
function(Backbone,
        NotificationForm,
        User,
        FormTpl) {

    var UserForm = Backbone.View.extend({
        el: "#user-form",

        template: _.template(FormTpl),

        initialize: function() {
            this.model = new User();
            this.model.on("invalid",this.showErrors, this);
            this.model.on('request',this.ajaxStart, this);
            this.model.on('sync',this.ajaxComplete, this);
        },

        events: {
            submit: 'register'
        },

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

        register: function(e) {
            e.preventDefault();
            var attrs = {
                counter: 0,
                created: new Date(),
                updated: new Date(),
                stackexchangeId: $('#stackId').val(),
                site: $('#site').find(":selected").text()
            };

            var self = this;
            this.model.save(attrs,
                {
                    wait:true,
                    success: function(model, response) {
                        console.log('success ajax');
                        console.log(model);
                        console.log(response);
                        self.collection.add(model);
                        //self.collection.add(new User(response));
                        var form = { id:'#user', messages: 'User has been registered' };
                        var success = new NotificationForm({ type: 'success', form: form} );
                        success.closeSuccessNotification();
                    },
                    error: function(model, xhr, options) {
                        self.ajaxComplete();
                        if(xhr.status == '500') {
                             var form = { id:'#user', messages: 'Connection StackExchange                  API failed' };
                        } else {
                            var response = JSON.parse(xhr.responseText);
                            var form = { id:'#user', messages: response.users };
                        }
                        new NotificationForm({ type: 'warning', form: form} );
                    }
                }
            );
        },

        showErrors: function(errors) {
            var form = { id:'#user', messages: errors.validationError };
            new NotificationForm({ type: 'error', form: form} );
        },

        ajaxStart: function() {
            this.$('#spinner-register').addClass('spinner-anim');
        },

        ajaxComplete: function() {
            this.$('#spinner-register').removeClass('spinner-anim');
        }
    });

return UserForm;
因此,当我再次单击时,我的模型是相同的,id在这里

我已经找到了一个解决方案,但我不认为这是一个好的解决方案,因为我必须将我的事件从initialize函数中移出。 所以我创建了:

test: function(model) {
            model.on("invalid",this.showErrors, this);
            model.on('request',this.ajaxStart, this);
            model.on('sync',this.ajaxComplete, this);
        },
register: function(e) {
            this.model = new User();
            this.test(this.model);
            e.preventDefault();
   etc.
 }
在寄存器中,我这样做:

test: function(model) {
            model.on("invalid",this.showErrors, this);
            model.on('request',this.ajaxStart, this);
            model.on('sync',this.ajaxComplete, this);
        },
register: function(e) {
            this.model = new User();
            this.test(this.model);
            e.preventDefault();
   etc.
 }
它工作得很好,但我完全删除了初始化函数,听起来不是很好。我希望保持我的第一个示例中的初始化,并且始终拥有一个新的用户模型。
谢谢:)

您可以使用
model.clear()
使模型像新的一样新鲜,然后在
save()
上发送POST请求

...
this.model.clear();
this.model.save(attrs,
...

谢谢它的作品,我错过了它的文档。我对代码做了一点修改,因为集合上的add事件只有一次是fire。