简体   繁体   English

如何从余烬控制器或组件观察车把模板中的多个状态

[英]How to observe multiple states in a handlebar template from an ember controller or component

I'm new to ember and am struggeling with the typical "how would one do that"-Problem. 我是新手,正在为典型的“人该怎么做”而苦苦挣扎。 What I've got is fairly simple and I know how to do it, but my way is so complicated that I do not think it's correct. 我所拥有的非常简单,我知道该怎么做,但是我的方法是如此复杂,以至于我认为这是不正确的。

The case: 案子:

<ul>
  <li>{{link-to top-level}}</li>
  <li>{{link-to another-top-level</li>
  <ul class="submenu">
     <li>{{link-to submenu</li>
  </ul>
</ul>

What should happen is: 应该发生的是:

  • When a route is clicked, the corresponding list element should become active. 单击路由后,相应的列表元素应变为活动状态。
  • When a submenu is clicked the corresponding upper ul-element should get the class open 单击子菜单时,相应的上级ul元素应打开该类

It's a fairly simple case with jQuery, but I understand that this is not scalable and abstracted and stuff. 这是使用jQuery的相当简单的情况,但是我知道这不是可伸缩的,抽象的东西。

Therefore I started with this approach: 因此,我从这种方法开始:

  • Create a controller / template construct for the entire navigation to handle it's state (there are some other things I need to check as well, so it came in handy). 为整个导航创建一个控制器/模板构造,以处理其状态(我还需要检查其他一些事情,因此派上了用场)。

  • since ember adds the active class to the anchor tag I created a component to observe that: 由于ember将活动类添加到了定位标记,所以我创建了一个组件来观察:

Like: 喜欢:

export default Ember.Component.extend({
  tagName: 'li',
  classNameBindings: ['active'],

  active: function() {
      return this.get('childViews').anyBy('active');
  }.property('childViews.@each.active') 
});

Replacing the li elements with {{linked-list}} does indeed work. 确实可以用{{linked-list}}替换li元素。

But what next? 但是接下来呢? Do I need to add another component to watch the component to watch the build in behaviour of active links? 我是否需要添加另一个组件来监视该组件以监视活动链接的内置行为? Do I have to write dedicated MVC-Classes for all the DOM Elements? 我是否必须为所有DOM元素编写专用的MVC类?

There has to be a simpler way, I think. 我认为必须有一种更简单的方法。 I already created a whole lotta files for such a simple behaviour that I'm thinking I'm totally on the wrong track. 我已经为这种简单的行为创建了一个完整的lotta文件,以至于我认为自己完全走错了路。

My gut feeling is: That is view logic and the view should just observe a few states in the template and that's it. 我的直觉是:这就是视图逻辑,视图应该只观察模板中的一些状态,仅此而已。

What's the leanest approach to the problem? 解决问题的最精简方法是什么?

I don't know if I understand your question right, but why you want to add the class open to the corresponding upper element? 我不知道我是否正确理解您的问题,但是为什么要向相应的上层元素添加open类? It automatically get active assigned. 它会自动被激活分配。 And with correct CSS it should work as expected. 并且使用正确的CSS,它应该可以按预期工作。

I have created a small example demonstrating what I mean. 我创建了一个小例子来说明我的意思。 Please have a look and let me know, if that's the solution for you or what's your problem with this solution. 请看一下,让我知道,如果这是适合您的解决方案,或者您对此解决方案有何疑问?

http://emberjs.jsbin.com/wifusosadega/7/edit http://emberjs.jsbin.com/wifusosadega/7/edit

EDIT 编辑

Here is a Bootstrap flavored version: http://emberjs.jsbin.com/wifusosadega/9/edit . 这是Bootstrap风格的版本: http : //emberjs.jsbin.com/wifusosadega/9/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM