Backbone.js 主干事件函数无法访问导致事件的更改对象
下面是一个用Typescript编写的backboneJS小程序,我试图根据模型中数据的变化触发各种函数。当函数被触发时,我很难让被触发的函数能够访问引起事件的已更改(添加、删除、修改等)的模型实例。在这个小程序中,我进行了一个小的API调用Backbone.js 主干事件函数无法访问导致事件的更改对象,backbone.js,typescript,Backbone.js,Typescript,下面是一个用Typescript编写的backboneJS小程序,我试图根据模型中数据的变化触发各种函数。当函数被触发时,我很难让被触发的函数能够访问引起事件的已更改(添加、删除、修改等)的模型实例。在这个小程序中,我进行了一个小的API调用e.change.get(“answer”),我很确定这是错误的,但我找不到正确的API调用 class Answers extends Backbone.Collection { constructor(options) { su
e.change.get(“answer”)
,我很确定这是错误的,但我找不到正确的API调用
class Answers extends Backbone.Collection {
constructor(options) {
super(options);
var self = this;
this.on("add", function (e) {
console.log("Added a new answer : " + e.change.get("answer")); // Need to access the newly added answer instance here
}, this);
}
model = Answer;
}
您应该能够使用访问它
this.on("add", function (model) {
console.log("Added a new answer : " + model);
});
第一个参数是模型(请参见)如果答案正确,请接受它,以便其他用户可以轻松找到正确答案。