【问题标题】:ember CollectionView set a className on last highlightedember CollectionView 在最后突出显示时设置一个 className
【发布时间】:2012-11-17 06:21:00
【问题描述】:

我有一个使用 Ember.CollectionView 显示的项目列表,其 tagName 为 ul。 我想将特定的 className 设置为获取 mouseOvered 的 li 元素,但在任何给定的情况下,只有一个元素可以拥有该类时间,基本上在任何元素的 mouseOver 上,我需要从所有元素中删除该类并将其添加到当前结束的特定元素中。

App.AnimalsListView = Ember.CollectionView.extend({
  tagName: 'ul',
  contentBinding: 'parentView.controller.content',
  classNames: ['animals'],
  elementId: 'animalsList',
  itemViewClass: Ember.View.extend({
    highlightNameBinding: "",
    classNameBindings: ['animal', 'highlightName'],
    template: Ember.Handlebars.compile(
      '{{view.content.title}}'
    ),
    mouseEnter: function() {
      // need to remove highlighted class on all elements
      // this.set('highlightName', 'highlighted'); on current element
    },
    mouseLeave: function() {

    }
  })
});

【问题讨论】:

    标签: ember.js collectionview


    【解决方案1】:

    您可以通过在鼠标进入或离开时切换布尔值来使用 className Bindings...

    App.AnimalsListView = Ember.CollectionView.extend({
      tagName: 'ul',
      contentBinding: 'parentView.controller.content',
      classNames: ['animals'],
      elementId: 'animalsList',
      itemViewClass: Ember.View.extend({
        isHighlighted: false,
        //highlight-name is the CSS Class to be applied when isHighlighted = true
        classNameBindings: ['animal', 'isHighlighted:highlight-name'],
        template: Ember.Handlebars.compile(
          '{{view.content.title}}'
        ),
        mouseEnter: function() {
          //due to the following line, class will be applied when mouse enters
          this.set('isHighlighted', true);
          //this.toggleProperty('isHighlighted'); if using the latest ember
        },
        mouseLeave: function() {
          //remove the class by making the value false when mouse leaves
          this.set('isHighlighted', false);
          //this.toggleProperty('isHighlighted'); if using the latest ember
        }
      })
    });
    

    希望这会有所帮助...

    点击时突出显示项目的更新

    如果您计划在单击列表中的项目时进行路由,并且不需要通过迭代清除布尔标志,则此方法特别有用...

    //define a reusable component this way
    
    App.ListView = Ember.CollectionView.extend({
        itemViewClass: Ember.View.extend({
            tagName: 'li',
            classNameBindings: ["isSelected:highlight-item"],
            isSelected: (->
                return this.get('content.id') === this.get('parentView.selected');
            ).property('parentView.selected')
        })
    })
    
    App.PostsController =  Ember.ArrayController.extend({
        content: [{
                name: "name1",
                id: 1,
                property1: "etc"
            },
            {
                name: "name2",
                id: 2,
                property1: "etc"                    
            }
            ]
    })
    
    //App.PostsView Handlebars
    {{#collection App.ListView contentBinding="content" selectedBinding="controllers.postController.content.id"}}
        Name: {{view.content.name}}<br>
        Comments: {{view.content.property1}}
    {{/collection}}
    {{outlet}} //to connect App.PostView
    
    //Now when an item is clicked you could send the context object or just the context id 
    

    如果你不使用路由,你可以按照 Aras 所说的去做

    【讨论】:

    • 非常感谢您的帮助,但我仍然缺少其中的一部分,因为在鼠标移出时,我只想让它将 highlight 设置为 false 如果另一个元素被突出显示,否则如果您将鼠标移出另一个元素它应该保持突出显示的页面部分
    • 我回答了如何在悬停时突出显示,让我更新一下如何在单击项目时突出显示...
    【解决方案2】:

    保留所选项目的一种方法是向您的 Animal 模型添加选择属性。然后,您可以在控制器中添加一个方法来迭代您的 animals 列表并将它们全部重置为 false

    clearHighlighted: function() { 
      animals.forEach(function(animal, index, self) {
        animal.set('highlighted', false);
      });
    }
    

    在鼠标输入时,您将首先调用该方法将所有突出显示的属性重置为false,然后将当前视图的highlighted 属性设置为true。不是最有效的,但我认为除了上面未指定的内容之外,这将相对容易实现。

    【讨论】:

    • 这是个好主意!这是正确的语法吗?在我的控制器中,我定义了 clearHighlighted: function() { this.get('content').every(function(item, index,self){ item.set('highlighted', false); }} 但由于某种原因,它似乎只迭代第一个项目然后退出我试图在那里放置一个控制台日志,它只循环一次,尽管我有 10 个内容...
    • 我想我明白了,如果我返回 true,它似乎可以工作;在每个函数中,这是正确的方法吗?
    • 你是对的,看起来如果回调函数返回 false,every 方法就会停止迭代。在这种情况下使用forEach 会好得多。我会更新我的答案。
    • @Unspecified 我应该更仔细地阅读 API。在这里使用setEach 会更优雅。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多