开发者

Does Knockout have a separator template?

When binding like this:

<div data-bind='template: { name: "personTemplate",
                            foreach: someOb开发者_开发百科servableArrayOfPeople }'> 
</div>

Is it possible to specify some sort of separator template, similar to the separator template in Classic ASP.NET?


There is not currently a way to specify a separator template.

However, I think that there are a few options:

  • include the content in your "personTemplate"
  • use a wrapper template that renders the personTemplate and then the separatorTemplate and point to it from your template binding (that way you could reuse the separator template, if necessary)
  • use the afterRender option of the template binding to insert the content.


If your needs are more presentational then behavioral (ie, a stylistic seperator only), and you dont need to support ancient browsers, you should consider if CSS could do the trick.

Imagine your DOM generated by KO looks like the following:

<div data-bind="template: bla bla">
    <div class="tmpl">template instance 1</div>
    <div class="tmpl">template instance 2</div>
    <div class="tmpl">template instance 3</div>
    <div class="tmpl">template instance 4</div>
</div>

You could insert "seperators" by using the ::after pseudo-class, and then turn it off for the last element.

.tmpl::after {
    content:'';
    display:block;
    background-color: silver;
    height: 2px;
    margin:5px 0;
}

.tmpl:last-child::after {
    display: none;
}

Depending on your use case, you can really do quite alot with generated blocks in CSS, and it's always a good day when you can cut some JS and put in CSS instead ;)

Exmaple fiddle http://jsfiddle.net/RTD7q/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜