2017-01-09 61 views
2

這是我的router.js代碼。如何訪問控制器中的參數ember.js

this.route('contact',{'path': '/contact/:chat_id'}); 

這是我的route.js代碼。

model(params) { 

    return this.store.findRecord("chat", params.chat_id) 
}, 

這是我的controller.js代碼,可以像這樣使用。它顯示錯誤爲空值 請幫助我。如何使用PARAMS在控制器

recordChat: function() { 

    var chat = this.get(params.chat_id) 

    Ember.RSVP.hash({ 
    offer_id: chat, 
    }) 
} 
+0

什麼錯誤?你應該考慮更新你的文章,以提供所需的信息。 – nyedidikeke

+0

它顯示空值 –

+0

你不會從'recordChat'函數返回任何東西。你什麼時候打電話給那個功能? – Lux

回答

0

在你route.js,你需要調用setupController功能是這樣的:通過調用

setupController(controller, model) { 
    this._super(...arguments); 
    controller.set('chat', model); 
} 

現在,您可以訪問它在你的控制器:

recordChat() { 
    const chat = this.get('chat'); 
    // if you don't call the setupController function, you could also do: 
    // const chat = this.get('model') or this.get('model.id') if you want the id only 
} 

UPDATE:

見WO rking twiddle這裏

+0

它doesn' t工作Bro –

+0

'this.get('chat')'或'this.get('model')'不返回任何東西?那不可能;)。你如何檢查它是否工作?你的recordChat目前沒有做任何事情...... –

+0

是Bro不起作用。這個rec​​ordChat將工作時,用戶填寫輸入框中的數據,並單擊保存按鈕,並在chat_id params將不得不填寫offerid的地方 –

3

我認爲最簡單的答案是路線創建一個變量,然後將其設置在setupController:

your_route.js

export default Ember.Route.extend({ 
    model(params) { 
    this.set('myParam',params.my_Params); 

    // Do Model Stuff... 
    }, 
    setupController(controller, model) { 
    // Call _super for default behavior 
    this._super(controller, model); 
    // Implement your custom setup after 
    controller.set('myParam', this.get('myParam')); 
    } 
}); 
+2

這裏'controllerFor'沒有必要,你可以說'controller.set(' – kumkanillam

+0

謝謝!不知道,我會改變! – Cameron

0

我想你可以獲取ID你需要使用模型,因爲你使用參數chat_id來查找記錄,並且該id現在是聊天對象(這是路由模型本身)的一部分。

所以,在你的控制器,你只需要做:this.get( '模型')的ID