javascript - Knockout js - && in if condition and containerless binding -


i displaying list of items , if items not available have display default message. now, have check whether object has been created , check object has list in it.

so now, doing below , works creates unnecessary dom elements. but, when same containerless binding doesn't seem work , there && syntax if in ko

<span data-bind="if: object">      <span data-bind="if: !object().property">          <p> list not available </p>     </span>  </span> // works   <!-- ko if: object -->      <!-- ko if: !object().property -->           <p> list not available </p>      <!-- /ko --> <!-- /ko -->  // doesn't work  

thanks

as mentioned codethug, using solutions provided display message until ko.applybindings have finished. more verbose solution, avoid problem without relying on css, use dynamic templates shown in following jsfiddle:

http://jsfiddle.net/sakb4/1/

this create valid markup inside virtual element when ko.applybindings done.

<!-- ko template: { name: dinamyclist } --> <!-- /ko -->  <script type="text/html" id="empty-template">   ... list not available markup ... </script>  <script type="text/html" id="list-template">   ... list available markup ... </script> 

being dinamyclist function returns name of template according verifications want valid list.

edit:

reading thru last comment made me think if behaviour want display "not avaiable template" after calculating list , property false, if thats case, following fiddle fix last 1 provide right condition.

http://jsfiddle.net/sakb4/3/

the "if" condition in template handle moment after knockout ready, before list is. if condition gets messy, advise put inside ko.computed clear markup.

<!-- ko template: { name: dinamyclist, if: object() !== undefined && object().property !== undefined } --> <!-- /ko --> 

Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -