Backbone 视图在引导箱中的 'this' 上下文

Backbone view's 'this' context in bootbox

我正在尝试在引导框模式中创建新对象。 我怎样才能在 bootbox 回调中访问 this.collection? 在我看来 _bind 会很有用,但我不知道如何。

以下发生在 Marionette.compositeView

create: function(evt) {
    console.log('create');
    evt.preventDefault();

    var modal = bootbox.dialog({
        title: "Nueva Seccion",
        message: Module.Templates['documents/create/course/chapter/chapterModal'],
        buttons: {
            success: {
                label: "Guardar",
                className: "btn-success",
                callback: function() {
                    var chapterNo = $('#chapterNo').val();
                    var chapterDesc = $('#chapterDesc').val();
                    var chapter = new Module.Models.Chapter({
                        chapterNo: chapterNo,
                        chapterDesc: chapterDesc,
                    });
                    var sub = new Module.Models.subChapter({});
                    chapter.get('subChapters').add(sub)

                    this.collection.add(chapter);

                }
            }
        }
    });

    modal.modal('show')
},

我通常会这样做,创建一个新变量(通常是 self)来保存正确的 this 值,就像这样:

    create: function(evt) {
    var self = this;
    console.log('create');
    evt.preventDefault();

    var modal = bootbox.dialog({
        title: "Nueva Seccion",
        message:  Module.Templates['documents/create/course/chapter/chapterModal'],
        buttons: {
            success: {
                label: "Guardar",
                className: "btn-success",
                callback: function () {
                    alert(self.collection);
                    var chapterNo = $('#chapterNo').val();
                    var chapterDesc = $('#chapterDesc').val();
                    var chapter = new Module.Models.Chapter({
                        chapterNo: chapterNo,
                        chapterDesc :chapterDesc,
                    });
                    var sub = new Module.Models.subChapter({});
                    chapter.get('subChapters').add(sub)

                    self.collection.add(chapter);

                }
            } 
        }
    });

    modal.modal('show');
}

希望对您有所帮助