title: access viewModel function in javascript function- knockout
body: <p>Have a problem accessing methods defined inside view model.</p>
  
  <p>Below are the view model definitions:</p>
  
  <pre><code>ProductMenu = function (name, subProductsMenu1, selectedMenu) {
      name = ko.observable(name);
      submenu = ko.observableArray(subProductsMenu1);
      selectedProductName = ko.observable();
  };
  ProductSubMenu = function () {
      submenuName = ko.observable();
      submenu2 = ko.observableArray([]);
      selectedSubMenuName = ko.observable();
  };
  ProductSubMenu2 = function () {
      submenu2Name = ko.observable();
      properties = ko.observableArray([]);
      selectedSubMenu2Name = ko.observable();
  };
  Properties = function () {
      productName = ko.observable();
      shortDesc = ko.observable();
      longDesc = ko.observable();
      additionalDocs = ko.observableArray([]);
  };
  var MainViewModel = function () {
      productModel = new ProductMenu();
      subMenuModel = new ProductSubMenu();
      submenu2Model = new ProductSubMenu2();
      propertyModel = new Properties();
      AllProductsModel = ko.observableArray([]);
  
     ReturnToFamilyProduct = ko.observable(true);
      ShowSubMenu = ko.observable(false);
      showSubMenu2 = ko.observable(false);
      ShowBackBtnOnSubMenuClick = ko.observable(false);
      IfDocumentsPresent = ko.observable(true);
  
      //to add product to products array
      AddProducts = function (name, subProductsMenu1, selectedMenu) {
          this.AllProductsModel.push(new ProductMenu(name, subProductsMenu1, selectedMenu));
      };
  };
  </code></pre>
  
  <p>In $(document).ready function i am trying to access the view model methods, the code is shown below:</p>
  
  <pre><code> var VM;
  
      $(document).ready(function () {
          VM = new MainViewModel();
          ko.applyBindings(VM);
  
          FetchProductFamiliesForProductsKO();
      });
      function FetchProductFamiliesForProductsKO() {
  
          $.getJSON(GetPath('/FetchProduct'), {}, function (data) {
              for (var i = 0; i &lt; data.length; i = i + 1) {
                  var family = data[i];
                  VM.AddProducts(family, null, family);
              }
          }).error(function () {
              console.log("Error occured");
          });enter code here
      };
  </code></pre>
  
  <p>I get an error in the function "FetchProductFamiliesForProductsKO" : uncaught typeerror: object[object object] has no method 'AddProducts'. </p>
  
  <p>How to access the MainViewModel methods? are the View model definitions correct?  and how to intialize the MainViewModel?</p>
  
  <p>Thanks in advance.</p>