Assigning 'active' class to selected list item in EmberJS

后端 未结 12 2085
星月不相逢
星月不相逢 2020-12-08 00:34

I have a list and I\'d like to set one item as class=\"active\" automatically. Given the following bootstrap code:

12条回答
  •  误落风尘
    2020-12-08 01:11

    I solved a similar problem by creating a view for each item and using classNameBindings (I have to say that i don't have a HTML list, i.e.... in my app, just list of

    ).

    Here is the way it works for me:

    In tasklist.handlebars i iterate over my custom view

      {{#each tasks}}
        {{view App.TaskListItemView contentBinding="this"....}}
      {{/each}}
    

    Ember will insert a view (i. e.

    ) for each item.

    The view class for each item is defined in task_list_item_view.js as

    App.TaskListItemView = Ember.View.extend({
    
      controller: null,
      classNameBindings: ['isSelected', 'isClosed'],
    
      isClosed: function() {
          var content = this.get('content');
          return content && !content.isOpen(new Date);
      }.property('controller.content.@each'),
    
      isSelected: function() {
          return (this.get('controller').isSelectedTask(this.get('content')));
      }.property('controller.taskSelection.@each'),
    
      ....
    });
    

    Finally the template for the view just renders my link in tasklistitem.handlebars

    
      ....
    
    

    AFAIK you have to specify the source data in the property() call to let ember know when to (re-) evaluate the property.

    Hope that helps

提交回复
热议问题