這個答案適用於2.x.x並寫成的2.15版本。
這聽起來像你剛剛在Ember開始。歡迎!我強烈建議你做Tutorial並閱讀The Guides。他們是一種投資 - 事情會變得更快,並且事後更容易。我要告訴你的一切都可以在那裏找到。它們不可跳過。
在你的用戶的路線:
model() {
return this.store.findAll('users')
}
在用戶控制器:
actions: {
consoleLogger(thingToLog) {
console.log(thingToLog)
}
}
數據和行動,從控制器和路線傳下來的部件。這是通過把手模板完成的。
傳球動作,模型和變量到組件的通用示例:
{{some-component
someControllerVariable=someControllerVariable
mode=model
consoleLogger=(action "consoleLogger")}}
然後在你的組件,你可以使用這樣的操作:
<button {{action consoleLogger "string to log"}}>Log it</button>
或者用它在你的組件js是這樣的:
this.get('consoleLogger')("string to log");
// this.get retrieves the function and then we call it with an argument, kind of like how you'd do someOtherFunction() in plain js
既然你需要顯示助手列表,你會需要你的路線,使用each helper:
{{#each users as |user|}}
{{user-list-item
someControllerVariable=someControllerVariable
user=user
consoleLogger=(action "consoleLogger")}}
{{/each}}
當動作在你的組件中使用,你可以把記錄作爲參數。他們將進入控制器操作,這就是您應該執行POST/PATCH/DELETE類型操作(如save())的位置。
祝你好運:)
感謝@handlebears的提示又冗長的答覆:)我已閱讀指南和教程幾次,但我想我有點忘了哪個用什麼。儘管您的詳細建議有幫助。再次感謝一堆。 – Ashokaditya
@Ashokaditya歡迎:) – handlebears