添加记录后,它不会立即向视图显示数据

时间:2014-02-24 06:48:20

标签: 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.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('change',this.render,this);
},
render: function(){
    this.$el.html(this.template(this.model.toJSON()));
    return this;
}
  });
})();

0 个答案:

没有答案