hunterloftis / knockout.namespaces

Namespaces plugin for KnockoutJS
knockoutjs.com
57 stars 5 forks source link

Question: how to use the non-global viewmodel in an {{IF}} block? #6

Closed scottmessinger closed 13 years ago

scottmessinger commented 13 years ago

How do I use the non-global viewmodel in an {{IF}} block? For instance, in the code below, nested_children is a property on the unit view model, not the global view model. How do I get it to work?

  {{if nested_children == false}}
    <p> stuff </p>
  {{else}}
    <p> other stuff </p>
  {{/if}}

Thanks! Scott

hunterloftis commented 13 years ago

Scott,

I can't speak directly to your problem because I've avoided messing with templating in KO too much (it's implemented in a complicated way, imho).

A workaround for your example that might let you avoid headaches is:

<p data-bind-namespace='visible: (nested_children() === false)'> stuff </p>
<p data-bind-namespace='visible: (nested_children() === true)'> other stuff </p>

In some cases, since namespacing is not part of the core design of KO, it can be like fitting a square peg into a round hole. I have considered implementing templating such that all templates would 'inherit' the namespace that called them. For example, you might use data-bind-namespace='template...' and then within the template data-bind='stuff' would automatically be bound to 'namespace.' That has the potential to avoid a lot of the complexity of KO templating, and could probably provide the right context for your issue above (using template statements like 'if').

Would that work within your application?

scottmessinger commented 13 years ago

Thanks for reply.

RE: data-bind-namesapce : "template ...' I thought that's how it worked now. At least, that how it seems to work. However, I've found that within that template, I can't pass templateOptions to any subtemplates.

RE: KO templating.... I see you're working on okay.js. How is that coming? Does it make templating simpler? Also, what do you think of DOMJuice? It seems like knockout.js for backbone.js...potentially a winning combination.

scottmessinger commented 13 years ago

A couple things...

"However, I've found that within that template, I can't pass templateOptions to any subtemplates." I found the problem: I wasn't putting a space between my curly braces }} !== } }.

Based on your comments, I've decided to just namespace my viewmodels within the master view model. It's easier and accomplishes the same goal without the hassle.

Thanks!