Search code examples
knockout.jsrequirejsdurandalsingle-page-applicationamd

compose bind data from Html to included html on search click single page application


I have a small requirement that i want to refresh products data after search product click event.

SearchProduct(search.html) search Click --> GetProduct --> pass data to include html(products.html)

<------- search.html ------>
    <!--ko compose: {view: 'products.html'} -->
    <!--/ko-->

<--- search.js --->
define(function (require) {

      return
      {
          Products: ko.observableArray(),
          searchProducts: function (data) {
                  $.get('Api', searchInputs, function (data) {
                     Products = data;
                  }
          }
      };
});

<---- products.html ---------->

<div>
    <table data-bind='visible: Products().length > 0'>
        <thead>
            <tr>
                <th data-bind="text: orderIdText"></th>
                <th data-bind="text: customerNameText"></th>
                <th data-bind="text: carrierText"></th>
                <th data-bind="text: soldAtStoreText"></th>
                <th data-bind="text: linesText"></th>
                <th data-bind="text: dateSoldText"></th>
            </tr>
        </thead>
        <tbody data-bind='foreach: Orders'>
            <tr>
                <td><a href="#" data-bind="text: OrderInfo.OrderNumber"></a></td>
                <td data-bind="text: CustomerInfo.FirstName"></td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </tbody>

    </table>
</div>

can one one help?

Thanks
shiva


Solution

  • <!--ko compose: {view: 'searchproducts.html', model:''} -->
    <!--/ko-->
    
    function ProductDetails()
    {
      // Properties..
    }
    
    $.get('Product/Search', searchObj, function (data) {
        var mapped = $.map(data, function (item) {
                return new ProductDetails();
        }
    }
    

    finally i got the above solution.