Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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
Backbone.js 将新项目添加到';添加';只触发一次_Backbone.js - Fatal编程技术网

Backbone.js 将新项目添加到';添加';只触发一次

Backbone.js 将新项目添加到';添加';只触发一次,backbone.js,Backbone.js,我是个新手,所以这可能是个愚蠢的错误。 当我按下发送按钮(#发送电子邮件按钮)时,一封电子邮件会按其应有的方式呈现 但当我再次按下时,没有更多的电子邮件添加。 我得到的唯一日志是:(第二次+推后) : 换句话说,它甚至不在集合中输入add处理程序。 有人能解释一下为什么以及如何解决这个问题吗 非常感谢! 编辑:如果我删除1封呈现的电子邮件,然后再次按“发送”,则新电子邮件添加正确 此处为相关代码: $(document).ready(function() { //email model

我是个新手,所以这可能是个愚蠢的错误。 当我按下发送按钮(
#发送电子邮件按钮
)时,一封电子邮件会按其应有的方式呈现

但当我再次按下时,没有更多的电子邮件添加。 我得到的唯一日志是:(第二次+推后) :

换句话说,它甚至不在集合中输入
add
处理程序。 有人能解释一下为什么以及如何解决这个问题吗

非常感谢! 编辑:如果我删除1封呈现的电子邮件,然后再次按“发送”,则新电子邮件添加正确

此处为相关代码:

$(document).ready(function() {
    //email model
    var EmailModel = Backbone.Model.extend({

        defaults: {
            id: '',
            email_from: '',
            email_recipient: '',
            email_subject: '',
            email_data: '',
            is_email_read: '',
            email_date: ''
        }


    });

    //email collection
    var email_collection = Backbone.Collection.extend({
        model: EmailModel,
        url: '/fetch_received_emails'
    });

    var email_collection = new email_collection();

    var EmailView = Backbone.View.extend({
        model: new EmailModel(),
        tagName:'li',
         events: {

             "click #email_template_view" : "click_email"
         },

        initialize: function() {
            console.log('initializing email view');
            this.template = _.template($('#email_template').html());
            console.log('finish initializing email view');
        },

        render: function() {

            this.$el.html(this.template(this.model.toJSON()));

            return this;
        },

         click_email: function() {
             this.model.is_email_read = true;
             $('#toggle_part_email').toggleClass('no_display');
         },

    });

    var CollectionView = Backbone.View.extend({
        model: email_collection,
        el: $('#emails_class'),
        initialize: function() {
            console.log('init to collection view');
            this.model.fetch();
            this.render();
            this.model.on('change', this.render, this);
            this.model.on('add', this.render, this);
            this.model.on('remove', this.render, this);

        },

        render: function(){
            console.log('rendering collection');
            var that = this,
                i;

            that.$el.html('');

            emails = this.model.toArray();

            for (i in emails){
                console.log(' printing emails');
                console.log(emails[i]);

                var new_email_view = new EmailView( {model : emails[i]});

                that.$el.append(new_email_view.render().$el);
            }
            console.log('about to exit collection view');
            return this;
        }
    });






    $('#send_email_button').click(function(event){

        // event.preventDefault();
        var sending_date= new Date();
        sending_date = sending_date.toDateString()

        //new email to ajax

        console.log('adding to collection');
        email_collection.add(new EmailModel({
                'email_from':$('#email_from').val(),
                'email_recipient' :$('#email_recipient').val(),
                'email_subject': $('#email_subject').val(),
                'email_data':$('#email_data').val(), 
                'is_email_read':false,
                'email_date': sending_date 
            }));
        console.log('about to exit');
        return false;
    });
    //create singelton for the collection view

   var c = new CollectionView();
});

为什么不尝试将单击事件用作另一个事件?在collectionView中,再次使用事件

events: {

             "click #send_email_button" : "AnyNameThatYouWant"
         },

AnyNameThatYouWant: function() {
            //Do all the things
         },

试试这个。

做到了。因为某种原因,任何你不想叫的名字。。。(在那里写下警告)代码必须在collectinView中。并确保“发送电子邮件”按钮位于视图El(电子邮件类)内。工作正常。非常感谢,伙计!美好的好!!如果你愿意,可以把我加入skype,也许我们可以分享知识。”xprenan’。
events: {

             "click #send_email_button" : "AnyNameThatYouWant"
         },

AnyNameThatYouWant: function() {
            //Do all the things
         },