簡體   English   中英

Ember:你如何從路由器訪問模型?

[英]Ember: how do you access the model from the router?

基於我所讀到的內容(如果我弄錯了,請糾正我),處理模型應該保存的邏輯以及下一步轉換的邏輯應該在路由器中。

如果是這種情況,我遇到了一個問題: 我不知道如何從路線訪問模型

這是我的控制器(並且在我按下提交后控制台記錄“CREATED”):

App.ScoutsNewController = Ember.ObjectController.extend
  submit: ->
    model = @get('model')
    model.on 'didCreate', ->
      console.log 'CREATED' # I want to  redirect to the index after creation
    model.save()

我應該將這條邏輯移到路線上,對嗎? 我們試試看:

App.ScoutsNewRoute = Ember.Route.extend
  model: ->
    App.Scout.createRecord()

  events:
    submit: ->
      # Based on what I've read, the right place to put the code you see in the controller is here. How do I get access to the model?
      # I have tried @get('model'), @get('content')

注意:我知道提交事件從視圖,控制器,然后最終路由起泡,停在任何一個已定義“提交”的路由。 因此,我想要路由來處理它,我刪除了控制器。 我能夠在路徑中看到任何console.log ,我只需要能夠訪問模型實例。

我正在使用Ember v1.0.0-rc.5-7-g610589a

謝謝!

兩個選項: this.currentModelthis.modelFor(routeName)

更新

我和SeñorAlexMatchneer談到了這件事。 this.currentModel沒有計划很快就會消失,但他認為this.modelFor(this.routeName)是公共API。

應該做些什么

this.controllerFor('ScoutsNew').get('content')

this.currentModel實際上並不是這里描述的批准方式

但在我的Ember版本(1.11)中, this.modelFor(this.routeName)返回null,所以這對我this.modelFor(this.routeName)

this.controllerFor(this.routeName).get('model')

你也可以使用this.controller.get('model'); 但有計划刪除控制器。

直到我們可以使用上面的代碼來檢索當前模型的路線

使用Ember 3.0.0,這是一種適用於我的文檔化方式:

const model = this.controller.model;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM