简体   繁体   English

Ember JS:为什么不执行该操作?

[英]Ember js: Why does the action not get fired?

I'm a noob in EmberJS. 我是EmberJS的菜鸟。 I have created an app using ember-cli. 我使用ember-cli创建了一个应用程序。

Use Case : I want to have navigation with two menus: Home Admin. 用例 :我想使用两个菜单进行导航:家庭管理员。 Admin has sub menus: Users Organizations. 管理员具有子菜单:用户组织。 On clicking Users, /users route and on clicking Organizations /organizations route should fire. 在单击“用户”时,/ users路由以及在单击“组织/组织路径”时应触发。 This is what I have done. 这就是我所做的。

In the application.hbs file , I have the following line: application.hbs file ,我有以下内容:

{{view 'main_menu'}}

this is the app/views/main_menu file: 这是app/views/main_menu文件:

import Ember from 'ember';
var MainMenu = Ember.CollectionView.extend({

  tagName: 'ul',
  classNames: ['nav', 'top-nav-menu'],
  content: function () {
   var result = [];
   result.push({label: "Dashboard", routing: 'dashboard', active: 'active'},
               {label: "Admin", routing: 'admin'});

  return result;

  }.property(),


 itemViewClass: Ember.View.extend({
   classNameBindings: ['active', ':top-nav-dropdown'],
   active: function(){
      return this.get('content.routing').indexOf('dashboard') === 0 ?"active":""; 
      return "";
    }.property(),
   templateName: 'main_menu',

    dropdownMenu: function () {
      var item = this.get('content').routing;
      var itemsWithDropdown = [ 'admin'];
      return itemsWithDropdown.contains(item);
    }.property(''),

    isAdminItem: function () {
      console.log("Inside is admin item");
      return this.get('content').routing == 'admin';
    }.property(''),


     dropdownCategories: function () {
      var itemName = this.get('content').routing;
      var categories = [];
      // create dropdown categories for each menu item
      if (itemName == 'admin') {
        categories = [];
        categories.push({
          name: 'users',
          url: 'users/',
          label: "Users"
        });
        categories.push({
          name: 'organizations',
          url: 'organizations/',
          label: "Organizations"
        });
      }
      return categories;
    }.property(''),

 AdminDropdownItemView: Ember.View.extend({
    // console.log("inside admin drop down item view");
     tagName: 'li',
     classNameBindings: 'isActive:active'.w(),
     isActive: function () {
       console.log("Inside the is active function");
       return this.get('item') === this.get('parentView.selectedAdminItem');
      }.property(),

      goToCategory: function (event) {
         console.log("inside admin drop down item view");
 /*I'm just printing something here to make sure control comes here before I proceed coding*/
       }
 })
})

});

export default MainMenu;

This is the app/templates/main_menu.hbs file: 这是app / templates / main_menu.hbs文件:

<a href= {{view.content.routing}} > 
  {{{unbound view.content.label}}}
</a>

{{#if view.isAdminItem}}
  <ul class="top-nav-dropdown-menu">
    {{#each category in view.dropdownCategories}}
      {{#view view.AdminDropdownItemView item="category.name" }}
        <a href="#" {{action "goToCategory" category.url target="view"}}>{{category.label}}</a>
      {{/view}}
    {{/each}}
  </ul>
{{/if}}

All other actions get called but the action goToCategory does not get called. 所有其他动作都被调用,但是动作goToCategory没有被调用。 Nor does it give error in the console that no action handler written as it usually gives one. 它也不会在控制台中产生错误,即通常不会编写任何动作处理程序来提供错误。

You need to put your actions in a hash called actions : 您需要将您的操作放入称为actions的哈希中:

AdminDropdownItemView: Ember.View.extend({
  ...
  actions: {
    goToCategory: function (event) {
      console.log("inside admin drop down item view");
    }
  }
})

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

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