javascript - 覆盖 TypeScript 中的值

标签 javascript backbone.js typescript

我正在做类似的事情:

class ProblemAnswersModel extends Backbone.Model {
    parse(response) {
        this.relations = {"interventions" : Backbone.Collection};
        this.set(response);
    }
}

但我想做:

class ProblemAnswersModel extends Backbone.Model {
    this.relations = {"interventions" : Backbone.Collection};
    parse(response) {
        this.set(response);
    }
}

我试过了

class ProblemAnswersModel extends Backbone.Model {
    relations = {"interventions" : Backbone.Collection};
    parse(response) {
        this.set(response);
    }
}

但这似乎创建了一个单独的变量,而不使用底层的主干变量。

最佳答案

如果您的意思是 Backbone.Collection 作为类型而不是构造函数,则应该像这样声明关系:

class ProblemAnswersModel extends Backbone.Model {
    relations: { "interventions" : Backbone.Collection };

    parse(response) {
        this.relations = { "interventions" : someBackboneCollectionInstance };
        this.set(response);
    }
}

如果你想向原型(prototype)添加关系:

ProblemAnswersModel.prototype.relations = ...

关于javascript - 覆盖 TypeScript 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31861715/

相关文章:

angular - 使用来自另一个模块的组件

javascript - 无法将变量传递给ajax url

javascript - Backbone 与 requirejs

javascript - Backbone - 无论如何检查一个事件之前是否已经绑定(bind)?

javascript - Angular 2 - 同时发出多个 HTTP 请求

javascript - TypeScript 在 WebStorm 7 中将所有 ts 文件编译为单个 JavaScript 文件

javascript - 从函数内调用 http.get 时不返回响应

javascript - 将 javascript 数组分配给具有属性名称的 JSON 对象列表?

javascript - 将总计从选择菜单选项获取到另一个表格单元格

backbone.js - 更新 Backbone.js View 变得一团糟