Javascript 添加记录后,不会立即将数据显示到视图中
当我加载页面时,它会获取所有数据,我会显示这些数据。但是,当我添加一条记录时,也就是说我正在提交表单“addcontact”,数据是在数据库中创建的。但它不会添加到集合中,并且不会触发this.collection.on('add')。所以,我认为问题就在于此。谁能告诉我哪里做错了?还有别的办法解决这个问题吗 这段代码在功能上起作用,但唯一的问题是,使用This.collection.create({new post},{wait:true})创建新记录;数据库中的值正在更新。但它并没有添加到集合中Javascript 添加记录后,不会立即将数据显示到视图中,javascript,php,backbone.js,backbone-views,backbone-events,Javascript,Php,Backbone.js,Backbone Views,Backbone Events,当我加载页面时,它会获取所有数据,我会显示这些数据。但是,当我添加一条记录时,也就是说我正在提交表单“addcontact”,数据是在数据库中创建的。但它不会添加到集合中,并且不会触发this.collection.on('add')。所以,我认为问题就在于此。谁能告诉我哪里做错了?还有别的办法解决这个问题吗 这段代码在功能上起作用,但唯一的问题是,使用This.collection.create({new post},{wait:true})创建新记录;数据库中的值正在更新。但它并没有添加到集
(function(){
Backbone.emulateHTTP = true;
//Backbone.emulateJSON = true;
window.App = {
Models : {},
Collections: {},
Views : {},
Router : {}
};
window.vent = _.extend({},Backbone.Events);
window.template = function(id){
return _.template( $('#'+id).html() );
};
//接触模型
App.Models.Contact = Backbone.Model.extend({
validate: function(attrs) {
if( !attrs.first_name ||
!attrs.last_name ||
!attrs.email_address) {
alert('Fill the missing fields');
}
}
});
//收藏
App.Collections.Contacts = Backbone.Collection.extend({
model: App.Models.Contact,
url : 'index.php/ContactsController/contacts'
});
//全局视图
App.Views.App = Backbone.View.extend({
initialize: function(){
vent.on('contact:edit',this.editContact,this);
//console.log(this.collection.toJSON());
App.addContactView = new App.Views.AddContact({collection: App.Contacts});
App.allContactsView = new App.Views.Contacts({collection: App.Contacts});
$('#allcontacts').append(App.allContactsView.el);
}
});
//添加联系人视图
App.Views.AddContact = Backbone.View.extend({
el: '#addcontact',
initialize: function(){
this.first_name = $('#first_name');
this.last_name = $('#last_name');
this.email_address = $('#email_address');
this.description = $('#description');
//this will fix it
this.collection.on("change", this.render , this);
},
events: {
'submit' : 'addContact'
},
addContact: function(e){
e.preventDefault();
this.collection.create({
first_name: this.first_name.val(), // <===== same as $this.el.find('#first_name')
last_name: this.last_name.val(),
email_address: this.email_address.val(),
description: this.description.val()
},{wait: true});
this.clearForm();
},
clearForm: function(){
this.first_name.val('');
this.last_name.val('');
this.email_address.val('');
this.description.val('');
}
//单个视图的视图
App.Views.Contact = Backbone.View.extend({
tagName: 'tr',
template: template('allContactsTemplate'),
initialize: function(){
this.model.on('change',this.render,this);
},
render: function(){
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
})();
这个问题是昨天提的吗?不完全是。它有一些变化,在这种情况下,指出它们是什么可能是个好主意。我不熟悉主干,但我怀疑如果这两个问题都得到了回答,90%的工作将会重复。我将添加另一个问题的链接。
App.Views.Contact = Backbone.View.extend({
tagName: 'tr',
template: template('allContactsTemplate'),
initialize: function(){
this.model.on('change',this.render,this);
},
render: function(){
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
})();