Backbone.js集合.Create函数没有使用wait: true返回更新后的模型
Backbone.js collection.create function doesn't return updated model with wait: true
在使用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)
相关文章:
- 对于loop.if-仅在经过所有间隔后返回true
- 如果函数返回True,则显示Javascript按钮
- Javascript If else 只返回 TRUE 或只返回 FALSE
- 过滤器返回true或false
- 我对“;返回true"嵌套函数内部;t工作
- 如何在成功输入时使“返回TRUE”
- 检查一个元素是否有一个类与另一个类总是返回true
- JavaScript函数未返回true
- 如果URL中有任何字符串变体匹配,则返回true
- 如何使以下函数返回true或false
- 使用“in”关键字检查 0 始终返回 true
- hasOwnProperty 在对照父对象属性进行检查时返回 true
- 为什么 isNaN 总是返回 true
- _.数组中的每个查找值都返回 true 或 false.使用下划线 JS
- 错误地返回true
- 为什么asp:LinkButton服务器端事件没有'当clientClick返回true时,t激发
- 阻止表单提交 javascript 为什么有一个明显的错误语句返回 true
- Node js命令器args返回true而不是值
- 如何将html表单与php链接,以便在例如单击按钮时返回true
- 从OnClientClick调用函数时,在JavaScript中返回true