Ember中的'action'没有调用控制器方法

时间:2013-09-13 18:18:55

标签: ember.js

我将此Ember Rails应用https://github.com/kagemusha/ember-rails-devise-demo从coffeescript转换为javascript。当我单击链接时,AuthController中的登录和注册操作完美地工作,但是,当我单击注销按钮时,我收到的错误是没有处理注销事件,即使有相同的操作设置也是如此控制器作为另外两个链接。

Uncaught Error: Nothing handled the event 'logout'. 

        {{view App.MenuItem href="#/login" label="Login" }}
        {{view App.MenuItem href="#/registration" label="Register"}}
         <button class="btn" {{action logout}}>Logout</button>

您能看出为什么AuthController中的注销操作可能无法处理该事件。它适用于coffeescript版本。

更新,有一个App.NavbarController,它扩展了ObjectController并将注销包装在动作哈希中。

Navbar Controller

  App.NavbarController = Ember.ObjectController.extend({
    needs: ['auth'],
    isAuthenticated: Em.computed.alias("controllers.auth.isAuthenticated"),
    actions: {
      logout: function() {   
        return this.get("controllers.auth").logout();
      }
    }
  });

身份验证控制器

App.AuthController = Ember.ObjectController.extend({
    currentUser: null,
    isAuthenticated: Em.computed.notEmpty("currentUser.email"),
    login: function(route) {
      var me;
      me = this;
      return $.ajax({
        url: App.urls.login,
        type: "POST",
        data: {
          "user[email]": route.currentModel.email,
          "user[password]": route.currentModel.password
        },
        success: function(data) {
          log.log("Login Msg " + data.user.dummy_msg);
          me.set('currentUser', data.user);
          return route.transitionTo('home');
        },
        error: function(jqXHR, textStatus, errorThrown) {
          if (jqXHR.status === 401) {
            return route.controllerFor('login').set("errorMsg", "That email/password combo didn't work.  Please try again");
          } else if (jqXHR.status === 406) {
            return route.controllerFor('login').set("errorMsg", "Request not acceptable (406):  make sure Devise responds to JSON.");
          } else {
            return p("Login Error: " + jqXHR.status + " | " + errorThrown);
          }
        }
      });
    },
    register: function(route) {
      var me;
      me = this;
      return $.ajax({
        // url: App.urls.register,
        url: "/users.json",
        type: "POST",
        data: {
          "user[name]": route.currentModel.name,
          "user[email]": route.currentModel.email,
          "user[password]": route.currentModel.password,
          "user[password_confirmation]": route.currentModel.password_confirmation
        },
        success: function(data) {
          me.set('currentUser', data.user);
          return route.transitionTo('home');
        },
        error: function(jqXHR, textStatus, errorThrown) {
          return route.controllerFor('registration').set("errorMsg", "That email/password combo didn't work.  Please try again");
        }
      });
    },
    logout: function() {
      var me;
      console.log("Logging out...");
      me = this;
      return $.ajax({
        url: App.urls.logout,
        type: "DELETE",
        dataType: "json",
        success: function(data, textStatus, jqXHR) {
          $('meta[name="csrf-token"]').attr('content', data['csrf-token']);
          $('meta[name="csrf-param"]').attr('content', data['csrf-param']);
          log.info("Logged out on server");
          me.set('currentUser', null);
          return me.transitionToRoute("home");
        },
        error: function(jqXHR, textStatus, errorThrown) {
          return alert("Error logging out: " + errorThrown);
        }
      });
    }
  });

2 个答案:

答案 0 :(得分:3)

您能展示更多模板和观点吗?在原始代码中,应用程序模板使用渲染标记来渲染NavBar,这将呈现NavBar模板并启动NavBar控制器。您可能希望将带有控制台日志的init函数放入控制器,以确保正确创建它:

init: function(){
    this._super();
    console.log('navbar controller starting...');
}

我注意到您发布的模板代码未包含{{#if isAuthenticated}}部分 - 这是否正常工作?

答案 1 :(得分:0)

操作需要包含在actions对象中。查看documentation了解详情。