Backbone.js集合.Create函数没有使用wait: true返回更新后的模型

Backbone.js collection.create function doesn't return updated model with wait: true

本文关键字:返回 true 更新 模型 wait Create 集合 js 函数 Backbone      更新时间:2023-09-26

在使用Backbone.js与Laravel 4时,我使用this.collection.create()与wait: true选项插入数据库。它插入到数据库中,但如果wait: true被设置,它不会更新模型。因此,如果我再次尝试输出集合,即使尝试成功回调函数返回值并且POST请求返回正值,它也不会更新。

该应用程序是一个联系人管理系统。这是我的联系人模型:

App.Models.Contacts = Backbone.Model.extend({
});

这是我添加联系人的视图

App.Views.AddContact = Backbone.View.extend({
el: '#addContact',
events: {
    'submit': 'addContact',
},
initialize: function() { // cache values
    this.first_name = $('#first_name');
    this.last_name = $('#last_name');
    this.email_address = $('#email_address');
    this.description = $('#description');
},
addContact: function(e) {
    e.preventDefault();
    this.collection = this.collection.create({
        first_name: this.first_name.val(),
        last_name: this.last_name.val(),
        email_address: this.email_address.val(),
        description: this.description.val(),
    }, { wait: true });
},
});

这是集合:

App.Collections.Contacts = Backbone.Collection.extend({
model: App.Models.Contacts,
url: '/projects/gamesapp/public/contacts',
});

这是页面上的脚本,将所有这些付诸行动:

<script>
    new App.Router;
    Backbone.history.start();
    App.contacts = new App.Collections.Contacts;
    App.contacts.fetch().then(function() {
        new App.Views.App({ collection: App.contacts });
    });
</script>

我怎么做才能使它工作?我想在视图中创建本地更新模型,它似乎没有。

谢谢!

问题不在js或主干代码中,而是在我写的Laravel代码中。

似乎POST返回了一个积极的响应和所有,但问题是在我的控制器的存储方法,你必须从store()函数返回create()函数!,说明一下:

public function store()
{
    $input = Input::all();
    return Contact::create(array( // YOU HAVE TO RETURN SOMETHING
        'first_name' => $input['first_name'],
        'last_name' => $input['last_name'],
        'email_address' => $input['email_address'],
        'description' => $input['description'],
    ));
}

我的问题发生了,因为我没有返回它,只是调用Contact::create()函数…(Contact是Laravel Model)