最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

I am displaying a list of items and if the items are not available I have to display a default message. Now, I have to check whether the object has been created and then check the object has the list in it.

So now, I am doing the below and it works it creates unnecessary dom elements. But, when I do the same with the containerless binding it doesn't seem to work and also is there an && syntax for if in KO

<span data-bind="if: object"> 
    <span data-bind="if: !object().property">
         <p> The list is not available </p>
    </span> 
</span> // Works 

<!-- ko if: object -->
     <!-- ko if: !object().property -->
          <p> The list is not available </p>
     <!-- /ko -->
<!-- /ko -->  // Doesn't work 

Thanks

I am displaying a list of items and if the items are not available I have to display a default message. Now, I have to check whether the object has been created and then check the object has the list in it.

So now, I am doing the below and it works it creates unnecessary dom elements. But, when I do the same with the containerless binding it doesn't seem to work and also is there an && syntax for if in KO

<span data-bind="if: object"> 
    <span data-bind="if: !object().property">
         <p> The list is not available </p>
    </span> 
</span> // Works 

<!-- ko if: object -->
     <!-- ko if: !object().property -->
          <p> The list is not available </p>
     <!-- /ko -->
<!-- /ko -->  // Doesn't work 

Thanks

Share Improve this question asked May 4, 2013 at 14:47 AshwinAshwin 2622 gold badges5 silver badges13 bronze badges 4
  • So "object" is a function? A function that when called returns an object? If not, then object().property is incorrect. – Pointy Commented May 4, 2013 at 14:49
  • Both version should and does work the same: jsfiddle/sAkb4. So you have your problem elsewhere... you should try to reproduce your problem in a jsfiddle... – nemesv Commented May 4, 2013 at 15:07
  • Yes, my issue is that when the dom loads the list not available shows momentarily even though the list is available since the object are calculated at the end.. – Ashwin Commented May 4, 2013 at 15:34
  • Perhaps you could CSS display: none; the span or p tag to make it not visible on the initial load, then use the visible binding? You should be able to use if: object && !object().property as well and eliminate one of your span tags (unless they are there for a reason). – kendaleiv Commented May 4, 2013 at 19:02
Add a ment  | 

3 Answers 3

Reset to default 2

As mentioned by CodeThug, using the solutions you provided would display the message until ko.applyBindings have finished. A more verbose solution, but that would avoid the problem without relying on CSS, is to use dynamic templates as shown in the following jsfiddle:

http://jsfiddle/sAkb4/1/

This will create the valid markup inside the virtual element only when the ko.applyBindings is done.

<!-- ko template: { name: dinamycList } -->
<!-- /ko -->

<script type="text/html" id="empty-template">
  ... list is NOT available markup ...
</script>

<script type="text/html" id="list-template">
  ... list is available markup ...
</script>

Being dinamycList a function that returns the name of the template according to the verifications you want for a valid list.

EDIT:

Reading thru your last ment made me think if the behaviour that you want is to display the "not avaiable template" only after calculating the list and the property is false, if thats the case, the following fiddle will fix the last one to provide the right condition.

http://jsfiddle/sAkb4/3/

The "if" condition in template will handle the moment after knockout is ready, but before the list is. If the condition gets too messy, i would advise to put it inside a ko.puted for a clear markup.

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

As you have seen, the element exists in the DOM, and it won't be removed until the ko.applyBindings call is finished. Thus the momentary display of this message.

I'd try hiding the entire section of the DOM and show a loading indicator instead. When ko/ajax is done, the loading indicator can then be removed and the markup you care about displayed.

Alternately, you could see if your page is taking a while to load and try to improve the load time of the page. The chrome profiling tools can help with this.

THis is very easy with my Convention over configuration lib

http://jsfiddle/VVb4P/

Just this and the lib will do the templatating for you

   this.items.subscribe(function(items) {
        if(items.length === 0) {
            this.items.push(new MyApp.EmptyViewModel());
        }
   }, this);
发布评论

评论列表(0)

  1. 暂无评论