无法获得财产'订阅'未定义或空引用

时间:2015-06-03 12:26:53

标签: javascript knockout.js

在我的HTML页面中订阅下拉列值更改时,我遇到了实现knockout Js的问题。这是我的HTML

    <div>
      <span>Request Types : </span>
      <select data-bind="options: requestTypes,
                    optionsText: 'Name',
                    optionsCaption: '--Select--',
                   value: selectedRequestType">
       </select>
    </div>

ViewModel如下所示

                        wd.viewModel = function () {
                        var data = {};
                        var self = this;
                        var url = '';
                        var selectedRequestingCountry = ko.observable("");
                        self.optionsProvider = new wd.optionsProvider(self);
                        self.objGenralInfo = new wd.GI_form(data);
                        /* For Requesting Country */
                        url = wd.ConfigObject.baseUrl + wd.ConfigObject.masterServiceUrl + wd.ConfigObject.category.importingCountries;     // Fetch Requesting Countries     
                        self.importingCountries = self.optionsProvider.get("importingCountries", url, {});
                        self.selectedRequestingCountry = ko.observable("");
                        this.requestingCountryArea = ko.observable("");

                        /* For Generic Name*/
                        url = wd.ConfigObject.baseUrl + wd.ConfigObject.masterServiceUrl + wd.ConfigObject.category.genericNames;     // Fetch Generic Names     
                        self.genericNames = self.optionsProvider.get("genericNames", url, {});
                        self.selectedGenericName = ko.observable("");

                        /* Override Requesting Country Address */
                        self.IsOverride = ko.observable(false);

                        /* For Request Types */
                        self.selectedRequestType = ko.observable("");
                        self.selectedTradeName = ko.observable("");
                    };

                    // Whenever the selected Generic Name changes, populate the Request Type
                    var myName = wd.viewModel.selectedGenericName.subscribe(function () {
                        wd.viewModel.url = wd.ConfigObject.baseUrl + wd.ConfigObject.masterServiceUrl + wd.ConfigObject.category.requestTypes + "/" +
                              wd.viewModel.selectedGenericName().Name;
                        wd.viewModel.requestTypes = wd.viewModel.optionsProvider.get("requestTypes", url, '');                                        // Fetch request types based on generic name
                    }, wd.viewModel);

                    $(function () {
                        ko.applyBindings(wd.viewModel);
                    });

只有在加载了通用名称下拉列表后才会填充“请求类型”下拉列表值。用户必须先选择“通用名称”。在页面加载时,它会抛出错误&#34;无法获得财产&#39;订阅&#39;未定义或空引用&#34;。

任何帮助都非常感谢,因为我是淘汰javascript MVVM框架的新手。

1 个答案:

答案 0 :(得分:2)

您已将viewModel声明为wd.viewModel = function(){},这是一个构造函数,您需要在绑定或订阅之前创建它的新实例。如果你添加这样的东西:

wd.viewModel = new wd.viewModel();

在这一行之前它应该起作用

  

var myName = wd.viewModel.selectedGenericName.subscribe(function(){