【发布时间】:2014-10-01 11:01:35
【问题描述】:
在将 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>
我该怎么做才能让它工作?我想在视图中创建以在本地更新模型,但它似乎没有。
谢谢!
【问题讨论】:
标签: javascript backbone.js laravel-4