0

鉴于这些片段(希望这个问题足够完整)......

ModelA.js(有很多modelB):

ModelBs = (function(_super) {
    ModelB.prototype.bWithName = function(name) {
        return this.find(function (b) {
            return b.name == name;
        });
    }
})(Backbone.Collection);

return ModelA = (function(_super) {
    ...
    ModelA.prototype.initialize = function() {
        this.modelbs = new ModelBs(this.modelbs, {});
    };

    ModelA.prototype.bWithName = function(name) {
        return this.modelbs.bWithName(name);
    };

    return modelA;
})(BaseModel);

ModelC.js(有一个模型A):

ModelC.prototype.toString = function(opts) {
    ...
    console.log(this.modelA); // defined...
    console.log(this.modelA.modelBs); // defined...
    console.log(this.modelA.bWithName("foo")); // undefined
    ...
}

在 ModelC.js 中,为什么是this.modelAthis.modelA.modelBs定义的,但this.modelA.bWithName()未定义,我该如何解决?

这有效:this.modelA.modelBs.first().

这将返回 undefined: this.modelA.modelBs.where({name:"foo"})

在 Web 控制台中,这有效:modelZ.modelAs.first().bWithName("foo").attributes.

访问器或方法通常不能通过其他模型获得吗?

谢谢-

4

1 回答 1

0

呸。该方法实际上是可用的,但严格的等于 ( ===) 正在扼杀搜索。在一种情况下,我试图使用错误的 typeof 名称进行搜索。

感谢您的输入!

于 2012-10-05T17:07:46.670 回答