0

Foreach Data Function getAvailableMethods

<!-- ko foreach: { data: getAvailableMethods(), as: 'item' } -->
<label style="position: relative;top: 2px;"  data-bind="attr: {'for': item.code}" class="label"><span data-bind="text: item.name"></span></label>
<!-- /ko -->

JS Code

 getAvailableMethods: function (methods) {

  console.log(methods);

   return methods;

  },

Currently, there is empty data on initialization ko foreach: { data: getAvailableMethods() But i want to populate the data every time variable methods gets updated How can i apply the binding of function getAvailableMethods() with variable methods ?

JS Code

  'use strict';

  var methods = '';

 getPaymentMethodLists: function () {

  $.ajax({

    success: function (data) {

      methods = data.methods

    }

  },

2 Answers 2

0

Use KnockoutJS observable array for that.

To initialise observable array, define your methods like this.

var methods = ko.observableArray();

And to populate methods with your AJAX response, you can use push(value) method, like this,

methods.push(method)

Refer to official documentation link for in depth details.

1
  • Still not working Commented Sep 6, 2022 at 10:17
0

It worked with the following way

Under payment.html

<!-- ko foreach: { data: getAvailableMethods(), as: 'item' } -->
<!-- /ko -->

Under payment-method.js

getAvailableMethods: function () {} which is getting the data via ajax request and it worked well.

Only problem occurs when there is some conditional logic error. Than, it breaks the flow of data binding, even though the object have updated data but the html section was not getting update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.