简体   繁体   English

单击或路由更改后的Ember Router活动项

[英]Ember Router active item on click or route change

I have a list of posts pulled from the server using Ember Datastore, which I then render in a sidebar list like view, each post generates a href using the {{action}} helper which generates a valid href by looking up the corresponding router state. 我有一个使用Ember Datastore从服务器中拉出的帖子列表,然后将其呈现在类似视图的侧边栏列表中,每个帖子都使用{{action}}帮助程序生成了href,该帮助程序通过查找相应的路由器状态来生成有效的href 。 Clicking on a Post loads it into a content outlet. 单击帖子可将其加载到内容输出中。 This all works perfectly. 所有这一切都完美。

I want to add a class to an item when it is selected — "is-active" for example — either when clicking on it, or when the router state transitions to the state which matches the href, for example: when the user presses the back button or navigates directly to /posts/24, a Post with ID 24 gets selected. 我想在选择某个项目时(例如,“处于活动状态”)将一个类别添加到一个项目中,或者在单击该项目时,或者当路由器状态转换为与href匹配的状态时,例如:当用户按下后退按钮或直接导航到/ posts / 24,将选择ID为24的帖子。

Ideally I would like the solution to this be separated from the actual Post record as it is purely a presentation thing and I want to also use this approach for highlighting menu items using the same principle. 理想情况下,我希望将其解决方案与实际的Post记录分开,因为它纯粹是一种演示,我也想使用此方法以相同的原理突出显示菜单项。

What is the generally accepted way to do this? 普遍接受的方法是什么?

Edit: 编辑:

App.Router: Ember.Router.extend(
  root: Ember.Route.extend(
    goToPostsIndex: Ember.State.transitionTo('posts.index')
    goToAbout: Ember.State.transitionTo('about')
    goToShowPost: Ember.State.transitionTo('posts.show')

    index: Ember.Route.extend(
      route: '/'
      redirectsTo: "posts.index"
    )

    posts: Ember.Route.extend(
      route: '/posts'

      index: Ember.Route.extend(
        route: '/'
        connectOutlets: ((router) ->
          router.get('applicationController').connectOutlet('posts', App.posts)
        )
      )

      show: Ember.Route.extend(
        route: '/posts/:post_id'

        connectOutlets: (router, post) ->
          router.get('postsController').connectOutlet('post', post)
     )
   )

     about: Ember.Route.extend(
       route: '/about'
       connectOutlets: (router) ->
         router.get('applicationController').connectOutlet('about')
     )

    )
  ) 
)

Here is a fiddle of my current setup: http://jsfiddle.net/Pts7Q/31/ 这是我当前设置的小提琴: http : //jsfiddle.net/Pts7Q/31/

If you provide a jsfiddle with example code you'll probably get a much more precise and complete answer to your question. 如果为jsfiddle提供示例代码,则可能会为您的问题提供更加精确和完整的答案。

With that said, one approach could be to create an "isSelected" Post attribute that is bound to a selectedPostController (or selectedPost computed property). 话虽如此,一种方法可能是创建绑定到selectedPostController(或selectedPost计算属性)的“ isSelected” Post属性。 On click or router/state initialization from url, you could set the value of selectedPostController to the selected item, triggering the bindings to update isSelected to true for the current post and false for all others. 在单击或从URL初始化路由器或路由器/状态时,可以将selectedPostController的值设置为选定的项目,从而触发绑定以将update更新为当前选定的isSelected为true,将其他所有绑定为false。

Then, following the ember docs at http://emberjs.com/documentation/#toc_binding-class-names-with-bindattr , 然后,按照http://emberjs.com/documentation/#toc_binding-class-names-with-bindattr上的ember文档进行操作,

"The class attribute can be bound like any other attribute, but it also has some additional special behavior. The default behavior works like you'd expect. “ class属性可以像其他任何属性一样绑定,但是它还具有一些其他特殊行为。默认行为的工作方式与您期望的一样。

If the value to which you bind is a Boolean, however, the dasherized version of that property will be applied as a class (if the boolean is true): 但是,如果绑定到的值是布尔值,则该属性的反划线版本将用作类(如果布尔值为true):

 <div {{bindAttr class="isSelected"}}>
   Post title here
 </div>

This emits the following HTML (if true it will be added, if false no class will be added): 这将发出以下HTML(如果为true,则将其添加,如果为false,则将不添加任何类):

 <div class="is-selected">
   Post title here
 </div>

Then style the is-selected CSS class to look however you want. 然后,将is-selected CSS类设置为所需样式。

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

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