Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.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
Javascript 在主干网中添加新记录时,它只添加到数据库,而不是集合_Javascript_Php_Database_Codeigniter_Backbone.js - Fatal编程技术网

Javascript 在主干网中添加新记录时,它只添加到数据库,而不是集合

Javascript 在主干网中添加新记录时,它只添加到数据库,而不是集合,javascript,php,database,codeigniter,backbone.js,Javascript,Php,Database,Codeigniter,Backbone.js,这段代码可以工作,但唯一的问题是,在使用This.collection.create{new post},{wait:true}创建新记录时,数据库中的值正在更新。但它并没有将其添加到集合中 我把完整的客户端代码放在下面。此外,我正在使用Codegniter框架进行数据库集成 (function(){ Backbone.emulateHTTP = true; //Backbone.emulateJSON = true; window.App = { Models:{},

这段代码可以工作,但唯一的问题是,在使用This.collection.create{new post},{wait:true}创建新记录时,数据库中的值正在更新。但它并没有将其添加到集合中

我把完整的客户端代码放在下面。此外,我正在使用Codegniter框架进行数据库集成

  (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);
},
editContact: function(contact){
    var editContact = new App.Views.EditContactView({model: contact});
    console.log(editContact.el);
    $('#editContactdiv').html(editContact.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')
},

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.Contacts = Backbone.View.extend({
tagName: 'tbody',   
initialize: function(){
    this.collection.on('add',this.addOne,this);
    this.render();
},
render: function(){
    this.collection.each(this.addOne,this);
    //console.log(this.el);

    return this;
},

addOne: function(contact){
    var ContactView = new App.Views.Contact({model: contact})
    //console.log(ContactView.render().el);
    this.$el.append(ContactView.render().el);
}

});
//单个联系人的视图

    App.Views.Contact = Backbone.View.extend({
tagName: 'tr',

template: template('allContactsTemplate'),
initialize: function(){
    this.model.on('destroy',this.unrender,this);
    this.model.on('change',this.render,this);
},
events: {
    'click a.delete': 'deleteContact',
    'click a.edit': 'editContact'
},
editContact: function(e){
    e.preventDefault();
    vent.trigger('contact:edit',this.model);
},
deleteContact: function(e){
    e.preventDefault();
    this.model.destroy();
},
render: function(){
    this.$el.html(this.template(this.model.toJSON()));
    return this;
},
unrender: function(){
    this.remove();
}
});

})();

是的,我在同一个集合中添加了instanceReaders:OP在这一个之后问了a,所以在这里花费大量精力之前,您可能希望看看是否/如何回答这个问题。
    App.Views.Contact = Backbone.View.extend({
tagName: 'tr',

template: template('allContactsTemplate'),
initialize: function(){
    this.model.on('destroy',this.unrender,this);
    this.model.on('change',this.render,this);
},
events: {
    'click a.delete': 'deleteContact',
    'click a.edit': 'editContact'
},
editContact: function(e){
    e.preventDefault();
    vent.trigger('contact:edit',this.model);
},
deleteContact: function(e){
    e.preventDefault();
    this.model.destroy();
},
render: function(){
    this.$el.html(this.template(this.model.toJSON()));
    return this;
},
unrender: function(){
    this.remove();
}
});

})();