Knockout Observable Array不是函数

时间:2016-07-10 02:36:01

标签: javascript knockout.js typescript

我试图访问Knockout Observable数组,但我收到一条错误消息,说它不是一个函数。我试图为我的viewmodel使用嵌套结构。当我尝试通过访问this.dsaModel().regions()来访问我的可观察数组时,我收到一条错误说"区域不是函数"。

我使用此调用从WebAPI收到JSON

module Sontag.Map.Services {
export class DsaDataValueService {
    private _baseUrl = '/umbraco/api/dsaapi/';

    getDsaData(): Q.Promise<Models.DsaModel> {
        return Q($.getJSON(this._baseUrl + 'getalldsadata'));
    }
}

}

JSON从API看起来像这样:

&#13;
&#13;
{
  "regions": [
    {
      "id": 1258,
      "name": "Canada",
      "institutes": [
        {
          "id": 1223,
          "name": "The Hospital for Sick Children",
          "location": "Toronto, Ontario Canada",
          "regionId": 1258,
          "latitude": null,
          "longitude": null,
          "recipients": [
            {
              "id": 1224,
              "name": "Michael D. Taylor",
              "title": "",
              "education": "M.D.,Ph.D.",
              "yearAwarded": 2007,
              "fieldOfStudy": "Cancer Genomics",
              "instituteId": 1223,
              "fieldOfStudyId": 1285,
              "regionId": 1258,
              "url": "/grants/brain-cancer/dsa-map/recipients/michael-d-taylor/"
            }
          ]
        }
      ]
    }
}
&#13;
&#13;
&#13;

目前我的viewmodel看起来像这样。

module Map.ViewModels {
    export class DsaViewModel implements IActivatable {      
        constructor(private _dsaDataValueService: Services.DsaDataValueService) {
        }

        public dsaModel = ko.observable<Models.DsaModel>();
        public filteredDsaData = ko.observable<Models.DsaModel>();
        public isActivated: boolean = false;


        activate() {
            this.getDsaData();
        }

        getDsaData() {
            this._dsaDataValueService.getDsaData()
                .done(data => {
                    this.dsaModel(data);

                });
        }
    }
}

模型看起来像这样。

module Map.Models {
    export class DsaModel {
        constructor(jsObject?: {}) {
            if (jsObject) {
                ko.mapping.fromJS(jsObject, {}, this);
            }
        }
        public regions = ko.observableArray<Models.RegionModel>();

        //Filter options
        public institutesSelectList = ko.observableArray<Models.SelectListModel>();
        public regionsSelectList = ko.observableArray<Models.SelectListModel>();
        public fieldsOfStudySelectList = ko.observableArray<Models.SelectListModel>();
    }
}

当我尝试像这样过滤可观察数组时出现问题:

var filteredRegions = ko.utils.arrayFilter(this.dsaModel().regions(),
                        region => region.id() === this.selectedRegion());
                    this.dsaModel().regions(filteredRegions);
                    return this.dsaModel();

当我通过&#34; this.dsaModel()。regions()&#34;作为ko.utils.arrayFilter调用的参数,我收到一条错误消息,提示&#34;区域不是函数&#34;

0 个答案:

没有答案