我试图在引导框模式内创建新对象。 我如何在 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');
}
希望这有帮助
关于javascript - Bootbox 中主干 View 的 'this' 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30878302/