Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionals #42

Closed
sachalifs opened this issue Aug 19, 2014 · 2 comments
Closed

Conditionals #42

sachalifs opened this issue Aug 19, 2014 · 2 comments

Comments

@sachalifs
Copy link

Is there any way of using a coditional on the template like in https://github.com/component/reactive#hiding-and-showing-elements?

Or for example on jade you would do:

- if (foo)
  .true True
- else
  .false False
@anthonyshort
Copy link
Contributor

With the current version that uses data-binding, it's usually easiest to just show/hide elements.

<div hidden="{{ someAttribute }}"></div>

Or inverse it

<div hidden="{{ !someAttribute }}"></div>

If you want to make a show/hide binding, it's not too hard. This example uses a <noscript> tag to act as a placeholder when you add/remove the element so it knows where to put it.

View.directive('if', {
  bind: function(node, view){
    this.parent = node.parentNode;
    this.placeholder = document.createElement('noscript');
  },
  update: function(value, node, view){
    if (value === false) {
      this.parent.replaceChild(this.placeholder, node);
    }
    else if (this.placeholder.parentNode) {
      this.parent.replaceChild(node, this.placeholder);
    }
  }
});

Might require a bit of work because that's directly off the top of my head :)

@sachalifs
Copy link
Author

Cool, thanks @anthonyshort !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants