knockout.js - Knockout ViewModel is being updated after ajax, but my foreach is not getting trigged -


i have view model. person retrieved ajax call

 var vm = ko.mapping.fromjs(persons, {});  vm.hobbies = ko.observablearray();  // other vm objects 

after viewmodel loaded display page, want load part(hobbies) view model (working)

// ...ajax call...  success: function(results){                 ko.utils.arrayforeach(results.hobbies, function(item) {                     vm.hobbies.push(item);                     });       debugger       }  // ...end ajax call... 

on debugger can see hobbies populated.

i have view loaded in on page load

<!--ko if: $data.hobbies-->     <div>       <ul class="fares-by-date-carousel" data-bind="foreach: hobbies()">             <li>2</li>       </ul>     </div> <!-- /ko --> 

at start portion not load (as expensive ajax call hasn't been called yet, fine) after vm.hobbies populated above html section still never displays.

not sure missing

the issue usage of:

<!--ko if: $data.hobbies--> 

this should used when scoping parent context being iterated over. example might like:

<!--ko foreach: person -->     <!--ko if: $data.hobbies--> 

in example $data represents each person, following if check assess if each person has hobby.

if not using structure (with no parent context) don't need scope using $data, can either use $root view model scope (which works when nested) or leave off together. both should work:

<!--ko if: $root.hobbies-->  <!--ko if: hobbies--> 

knockout binding context

$parent - view model object in parent context, 1 outside current context. in root context, undefined.

$root - main view model object in root context, i.e., topmost parent context. it’s object passed ko.applybindings. equivalent $parents[$parents.length - 1].

$data - view model object in current context. in root context, $data , $root equivalent. inside nested binding context, parameter set current data item (e.g., inside with: person binding, $data set person). $data useful when want reference viewmodel itself, rather property on viewmodel.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -