Knockout Observable Array Length始终为0

Str*_*ake 9 html javascript mapping ko.observablearray knockout.js

在customerOverview视图模型中调用任何observable的长度时,我会收到零长度.随着绑定随数据更新,可观察数据中存在数据,但长度保持为0.基本视图模型'CustomerCentral'正确返回长度.我需要'CustomerOverview'中一些observable的长度来做一些条件语句.

HTML绑定

<ul class="nav nav-list">
      <li class="nav-header">Contacts</li>
      <!--ko  if: customerOverview.contacts().length == 0-->
      <li>No contacts associated with this customer</li>
       <!-- /ko -->
      <!--ko  foreach: customerOverview.contacts()-->
      <li>
      <a data-bind="click: $root.customerOverview.viewContact"><i class="icon-chevron-                        right single pull-right">
       </i><span data-bind="text: FirstName"></span><span data-bind="text: LastName"></span>
      </a></li>
      <!-- /ko -->
</ul>
Run Code Online (Sandbox Code Playgroud)

JS

function CustomerOverview() {
        var self = this;        

        self.contacts = ko.observableArray([]);           

        self.getCustomerContacts = function () {
            requestController = "/CRM/CustomerCentral/CustomerContacts";
            queryString = "?id=" + self.customer().Id();
            $.ajax({
                cache: false,
                type: "GET",
                dataType: "json",
                url: baseURL + requestController + queryString,
                headers: { "AuthToken": cookie },
                success:
                        function (data) {
                            if (data.data.length > 0) {

                                self.contacts(ko.mapping.fromJS(data.data));

                                console.log(self.contacts().length);
                            }
                        }
            });
        };
};
   function CustomerCentral() {

        var self = this;

        self.customerOverview = ko.observable(new customerOverview());
};

var vm = new CustomerCentral();
    ko.applyBindings(vm);
Run Code Online (Sandbox Code Playgroud)

Console cmd:vm.customerOverview().contacts().length 0

---------------------------解决方案---------------------- observableArray.push()

问题结果是这一行:

  self.contacts(ko.mapping.fromJS(data.data));
Run Code Online (Sandbox Code Playgroud)

解决方案:在此处添加.push()可以使数组的length属性递增.我曾假设ko.mapping会处理这个问题,但事实并非如此.将变量更改为observable无效.

 $.each(data.data, function () {
          self.contacts.push(ko.mapping.fromJS(this));
           console.log(self.contacts().length);
                                    });
Run Code Online (Sandbox Code Playgroud)

rom*_*n m 6

我认为你的问题不是你的customerOverview财产是可观察的

尝试:

 self.customerOverview = ko.observable(new CustomerOverview());
Run Code Online (Sandbox Code Playgroud)

要么

 self.customerOverview = ko.computed(function(){
       return new CustomerOverview();
 });
Run Code Online (Sandbox Code Playgroud)

工作样本:

http://jsfiddle.net/dvdrom000/RHhmY/1/

HTML

<span data-bind="text: customerOverview().contacts().length"></span>
<button data-bind="click: customerOverview().getCustomerContacts">Get contacts</button>
Run Code Online (Sandbox Code Playgroud)

JS

function CustomerOverview() {
        var self = this;        

        self.contacts = ko.observableArray([]);           

        self.getCustomerContacts = function () {
            self.contacts.push(1);
            self.contacts.push(2);
            self.contacts.push(3);            
        };
};
   function CustomerCentral() {

        var self = this;
        // Is this a correct way. Am I breaking something with this?
        self.customerOverview = ko.observable(new CustomerOverview());
};

var vm = new CustomerCentral();
    ko.applyBindings(vm);
Run Code Online (Sandbox Code Playgroud)


Str*_*ake 2

observableArray.push()

问题原来是这一行:

self.contacts(ko.mapping.fromJS(data.data));
Run Code Online (Sandbox Code Playgroud)

解决方案:向其中添加 .push() 可以使数组的长度属性增加。我原以为 ko.mapping 会处理这个问题,但事实并非如此。将变量更改为 observable 没有效果。

$.each(data.data, function () {
          self.contacts.push(ko.mapping.fromJS(this));
           console.log(self.contacts().length);
                                    });
Run Code Online (Sandbox Code Playgroud)