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,

ramo
  • 35
  • 4

1 Answers1

0

The best thing you could probably do is to create a ko.computed function which will return a new array with the output you desire. You can then bind the computed.

You can either do the looping logic yourself and output a new array, or if you are lazy you can use underscore.js (_) which if i remember correctly has a lot of nice functions to do things for you automatically with arrays.

Piotr Stulinski
  • 9,241
  • 8
  • 31
  • 46