1

我正在尝试为 AngularJS 应用程序创建一个丰富的数据模型,我希望我的模型继承自Array<BaseModel>. 我还没有找到一种方法来做到这一点(有信心)。

在伪代码中,这将类似于:

// NOT REAL CODE. DOES NOT WORK
class BaseModel {}
class Person extends BaseModel {}
class People extends Array<Person> {
    constructor(private promise:Promise) { }
    $unwrap(promise) { 
        promise.then((response) => {
            response.data.map((person) => {
                this.push(new Person(person));
            });
        });
    }
    static $load() {
        /* do some ajaxy thing and unwrap the promise right onto the 
           the new instance of this rich model */

        return new People(promise);
    }
}

我想要这样的原因是现在我可以直接将此模型绑定到我的视图并即时获取更新。

关于从 Array 扩展的任何想法?

4

1 回答 1

0

此时不可能从 TypeScript 中的 Array 继承。最好解包成一个公共数组字段并使用该字段绑定到 UI。它解析为相同的功能,并消除了从 Array 继承的必要性。

于 2015-02-04T08:13:00.293 回答