0

I have two array of Knockout selectedSocialNetworks and List_2 all of this list can contain one social network (twitter or facebook) or both (twitter and facebook)

<script id="socNetchoiceTmpl" type="text/html">         
 <ul data-bind="foreach: socialNetworksList, visible: socialNetworksList().length > 0">
     <li>
        <input type="checkbox" data-bind="value: $data, checked:  
            $parent.selectedSocialNetworks" /><span data-bind="text: $data"/>
    </li>
 </ul>

  function SocialNetChoicesViewModel() {
    var self = this;
    self.socialNetworksList = ko.observableArray([]);
    self.selectedSocialNetworks = ko.observableArray([]);
    self.List_2 = ko.observableArray([]);        

    self.save = function () {
      if(....){
            $.ajax("/Home/SocialNetworksChoice", {
              data: ko.toJSON({ selectedSocialNetworks: self.selectedSocialNetworks }),
              type: "post", contentType: "application/json",
              success: function (result) {
                if (result.Success) {
                    //alert(result.Message);
                }
                else {
                    alert(result.Message);
                }
              }
            });
       }
       else{
          $.ajax("/Home/Authentification", {
              data: ko.toJSON({ socialNetworks: self.List_2 }),
              type: "post", contentType: "application/json",
              success: function (result) {
                if (result.Success) {
                    //alert(result.Message);
                }
                else {
                    alert(result.Message);
                }
              }
            });
       }
    };
    // Load initial state from server, convert it to Task instances,
    // then populate self.tasks
    $.getJSON("/Home/SocialNetworksChoice", function (allData) {
        var mappedItems = $.map(allData, function (item) { return item });
        self.socialNetworksList(mappedItems);
    });

    $.getJSON("/Home/Authentification", function (allData) {
        var mappedItems = $.map(allData, function (item) { return item });
        self.List_2(mappedItems);
    });
 };

I want to detect wish social network doesn't contain in my List_2 and its in selectedSocialNetworks

I m using if than else to detect if List_2 = selectedSocialNetworks execute instruction in if ...

I'm Sorry for my bad english,

thanks,

4

1 回答 1

0

您可能做的最好的事情是创建一个 ko.computed 函数,该函数将返回一个具有您想要的输出的新数组。然后,您可以绑定计算的。

你可以自己执行循环逻辑并输出一个新数组,或者如果你很懒,你可以使用 underscore.js (_),如果我没记错的话,它有很多很好的函数可以自动为你处理数组。

于 2015-01-14T13:34:42.210 回答