Best way to implement a tabpanel in ember?

I'm new to ember and trying to build a Ember driven web application. I've read various tuts and studies several examples. The basic concepts are clear but now I'am stuck on trying to implement a tabpanel. My approach is as follows:

View

Configurator.TabPanelView = Ember.View.extend({
    classNames: ['tabPanel'],
    templateName: 'tabPanel'
});

Template

<script type="text/x-handlebars" data-template-name='tabPanel'>
  <div class='tabHead'>
      <ul>
          {{#each tabViews}}
          <li {{action "{{this.actionName}}" target="{{this.value}}"}} >{{this.title}}</li>
          {{/each}}
      </ul>
      <div class="tab-content">{{outlet}}</div>
  </div>
</script>

Usage in App

var tab= Configurator.TabPanelView.create({

            classNames: ['assortment'],
            tabViews: [{ title: 'First', value:'Foo', actionName: 'firstTab' },{title: 'Second', value:'Foo', actionName: 'secondTab' }],

            firstTab: Ember.View.extend({
                templateName: 'first'
            }),
            secondTab: Ember.View.extend({
                templateName: 'second'
            })
        });
        tab.appendTo("body");

The TabTemplate is rendered correctly but if I try to click on the li-elements following error is thrown

Uncaught Error: assertion failed: Target <(subclass of Ember.View):ember217> does not have action {{this.actionName}}

I'm also curious if I should use a router to implement tabbing. But as far as i can see routers act on application level and are intended to be used in single UI-compos.


The problem is in your template:

<li {{action "{{this.actionName}}" target="{{this.value}}"}} >{{this.title}}</li>

AFAIK, actions can't be bound, so when you write this, it tries to call the method {{this.actionName}} instead of firstTab , for example.

I think this is a typical example where you should use a Ember.CollectionView with an itemViewClass which has the click method, ie:

App.MyCollectionView = Ember.CollectionView.extend({
  tagName: 'ul',
  templateName: 'the-template-name',
  itemViewClass: Ember.View.extend({
    click: function() {
      var actionName = this.get('content.actionName'),
          target = this.get('controller.target');
      target.send(actionName);
    }
  })
});

The code above is surely not right, but the idea is here.

But I think the Router is the right way to do that. I suggest you to take a look at the Ember Router example by @ghempton, where it defines tab with Ember.Router .


You have 2 options:

1) each tabpage has its own controller, view and must also be defined in the router

<script type="text/x-handlebars" data-template-name="tabs"> 
    <div> 
      <ul class="nav nav-tabs"> 
        {{#view Bootstrap.TabItem item="info"}} 
          <a {{action gotoInfo}}>Info</a> 
        {{/view}} 
        {{#view Bootstrap.TabItem item="anamnese"}} 
          <a {{action gotoAnamnese}}>Anamnese</a> 
        {{/view}} 
        {{#view Bootstrap.TabItem item="medication"}} 
          <a {{action gotoMedication}}>Medication</a>  
        {{/view}} 
      </ul> 
      {{outlet}} 
    </div> 
</script>      

Bootstrap.TabItem = Ember.View.extend({
    tagName: 'li',
    classNameBindings: ['isActive:active'],

    isActive: function() {
        return this.get('item') === this.get('controller.selectedTab');
    }.property('item', 'controller.selectedTab').cacheable()
}); 

2) all tabpages are in one large view, and tabpages will be hidden or shown

{{#view Ember.TabContainerView currentView="info"}}
  <ul class="nav nav-tabs">
    {{#view Bootstrap.TabView value="info"}}<a>Info</a>{{/view}}
    {{#view Bootstrap.TabView value="anamnese"}}<a>Anamnese</a>{{/view}}
    {{#view Bootstrap.TabView value="medication"}}<a>Medication</a>{{/view}}
  </ul>
  {{#view Ember.TabPaneView viewName="info"}}
  {{view EEPD.InfoView}}
  {{/view}}
  {{#view Ember.TabPaneView viewName="anamnese"}}
  {{view EEPD.AnamneseView}}
  {{/view}}
  {{#view Ember.TabPaneView viewName="medication"}}
  {{view EEPD.MedicationView}}
  {{/view}} 
{{/view}}

Bootstrap.TabView = Ember.TabView.extend({
    tagName: 'li',
    classNameBindings: ['isActive:active'],

    isActive: function() {
        return this.get('value') === this.get('tabsContainer.currentView');
    }.property('tabsContainer.currentView').cacheable()
});

There are two ways to implement a tab panel.

If you want your tabs to be bookmarkable, then you should implement them using Router:

Templates

<script type="text/x-handlebars" data-template-name="application">  
  <div class="tabpanel">
    <div class="tabs">
      <div {{action "goToFirstTab"}}>First tab</div>
      <div {{action "goToSecondTab"}}>Second tab</div>
    </div>
    {{outlet}}
  </div>
</script> 

<script type="text/x-handlebars" data-template-name="firstTab">
  First Tab content
</script>

<script type="text/x-handlebars" data-template-name="secondTab">
  Second Tab content
</script>

Code :

var App = Ember.Application.create();
App.ApplicationController = Ember.Controller.extend();
App.ApplicationView = Ember.View.extend();
App.FirstTabView = Ember.View.extend({templateName: "firstTab"});
App.FirstTabController = Ember.Controller.extend();
App.SecondTabView = Ember.View.extend({templateName: "secondTab"});
App.SecondTabController = Ember.Controller.extend();
App.Router = Ember.Router.create({
  root: Ember.Route.extend({
    goToFirstTab: Ember.Route.transitionTo("firstTab"),
    goToSecondTab: Ember.Route.transitionTo("secondTab"),
    index: Ember.Route.extend({
      route: "/",
      redirectsTo: "firstTab"
    }),
    firstTab: Ember.Route.extend({
      route: "/firstTab",
      connectOutlets: function (router) {
        router.get('applicationController').connectOutlet('firstTab');
      }
    }),
    secondTab: Ember.Route.extend({
      route: "/secondTab",
      connectOutlets: function (router) {
        router.get('applicationController').connectOutlet('secondTab');
      }      
    })    
  })
});
App.initialize(App.Router);

The second way, without Router.

Templates (note that actions` targets are changed)

<script type="text/x-handlebars" data-template-name="application">  
  <div class="tabpanel">
    <div class="tabs">
      <div {{action "goToFirstTab" target="controller"}}>First tab</div>
      <div {{action "goToSecondTab" target="controller"}}>Second tab</div>
    </div>
    {{outlet}}
  </div>
</script> 

<script type="text/x-handlebars" data-template-name="firstTab">
  First Tab content
</script>

<script type="text/x-handlebars" data-template-name="secondTab">
  Second Tab content
</script>

Code (pretty much the same, except that the code related to tabs is now moved to ApplicationController.

var App = Ember.Application.create();
App.ApplicationView = Ember.View.extend();
App.Router = Ember.Route.create();
App.FirstTabView = Ember.View.extend({templateName: "firstTab"});
App.FirstTabController = Ember.Controller.extend();
App.SecondTabView = Ember.View.extend({templateName: "secondTab"});
App.SecondTabController = Ember.Controller.extend();
App.ApplicationController = Ember.Controller.extend({
  view: App.FirstTabView.create(),
  goToFirstTab: function () {
    this.connectOutlet("firstTab");
  },
  goToSecondTab: function () {
    this.connectOutlet("secondTab");
  }
});
App.initialize(App.Router);
链接地址: http://www.djcxy.com/p/11282.html

上一篇: 在Windows上运行grunt.cmd有多个问题

下一篇: 在烬中实现tabpanel的最佳方式是什么?