2015-04-20 46 views
1

我在Ember Cli版本:0.2.3Ember控制器奇怪的錯誤

我目前正在探索與Ember,並嘗試一個非常簡單的應用程序。

我在模板中有這樣的/ about.hbs

<h1>About</h1> 

<p>I'll write something interesting here</p> 

<button type="btn btn=primary"{{action 'clickMe'}}>Click me!</button> 

,並在我的控制器/ about_controller.js

Blogger.AboutController = Ember.Controller.extend({ 
    actions: { 
    clickMe: function() { 
     alert('Something something'); 
    } 
    } 
}); 

當我按一下按鈕我得到這個錯誤,我查了語法,似乎無法捕捉到錯誤。我需要一些新鮮的眼睛。

Uncaught Error: Nothing handled the action 'clickMe'. If you did handle the action, this error can be caused by returning true from an action handler in a controller, causing the action to bubble. 

現在,我明白了信息的意思,但我不知道如何解決它。

+1

[命名約定](http://www.ember-cli.com/#naming-conventions)很重要,文件名應該是'about.js',你應該使用es6模塊。 –

+0

@Kitler當我第一次創建about控制器我有它命名about.js,雖然我得到了這個錯誤 '未捕獲的錯誤:預計發現:'控制器:關於'博客/控制器/約'內''但'未定義' 。你忘了在'博主/控制器/'關於'嗎?'出口默認'?' – Diego

+0

你忘了導出嗎? :P –

回答

2

Ember大概找不到你的控制器。您可以使用ember inspector進行調試。無論如何,您應該在您的ember cli項目中使用ES6約定。你可以更多地瞭解這個主題here

無論如何,嘗試通過鍵入

ember generate controller about 

創建使用命令行about控制器,然後手動添加你的行動。

+0

像魅力一樣工作。儘管代碼非常相似,除了'''Blogger.AboutController'''改爲'''export default''' 感謝@Mateusz Nowak – Diego

2

您是否在爲您的程序使用ember cli或Ember入門工具包。

如果您使用的灰燼CLI,那麼你應該創建一個類似的文件 - 應用/模板/ about.hbs 應用程序/控制器/ about.js ,你應該在應用程序的一個條目/ router.js太[我想你必須那樣做。] 項在router.js-

Router.map(function() { 
    this.route('about'); 
}); 

現在你的模板/ about.hbs應該是─

<h1>About</h1> 

<p>I'll write something interesting here</p> 

<button {{action 'clickMe'}}>Click me!</button> 

和控制器/ about.js應該是─

import Ember from 'ember'; 
export default Ember.ObjectController.extend({ 
actions:{ 
     clickMe: function() { 
     alert('Something something'); 
    } 
    } 
}); 

使得所有上述修改後的執行CMD命令如下 - 燼服務器(這應該啓動服務器)

,如果你在上面的圖案做你的代碼應該工作。 命名約定在Ember中非常重要。

+0

是的,控制器的命名是不正確的。正如@Kitler提到的(ES6),全局變量已被ES6模塊取代。可悲的是,大多數例子都被棄用了。感謝您查看這個 – Diego