2012-11-03 50 views
3

我想從視圖對象上設置的底層對象獲取記錄ID,所以我可以將它設置在視圖的HTML包裝上。這裏就是我想:如何訪問Ember.View中的上下文?

// Row in record editing table view. 
views.RecordActionTableRowView = Ember.View.extend({ 
    // Does not work. 
    attributeBindings: ['record_id:data-record-id'], 
    templateName: 'pult-record-action-table-row', 

    init: function() { 
    console.log(this); 
    // Does not work either. Undefined. 
    console.log(this.get('record_id')); 
    // Does not work either. Undefined. 
    console.log(this.record); 
    return this._super(); 
    } 
}); 

這種觀點是從模板調用,所以它自己的模板中包含了正確的數據,但我無法找到它的視圖代碼內。有什麼建議麼?

+1

能爲您提供的jsfiddle說明問題了嗎?從發佈的代碼中,似乎不可能知道發生了什麼。你的模板是什麼樣的?記錄是如何設置的?你可以用這個作爲起點:http://jsfiddle.net/pangratz666/DvdVH/ – pangratz

+0

那麼,確切的例子在這裏幾乎沒有關係。我只是想弄清楚是否有一種通用的方式來從視圖中訪問模板上下文。關於它的文檔沒有任何內容。據說在視圖上有一個上下文屬性,但這也是未定義的。 – mikl

+0

你可以先調用_super(),然後是日誌? –

回答

3

你可能想this.get('context')this.get('content')。在某些情況下,如果在設置了context之前創建視圖,則在didInsertElement內檢查此內容可能會比在init內更好。

0

我justfaced試圖訪問某個文件中描述了同樣的問題在我的情況templateVariables以下工作

this.get("templateData.keywords.myvarname") 
+0

這不是公共API,應該避免。 –

+0

我完全同意你@LukeMelia然而公共Api中看起來不工作... [doc](http://emberjs.com/guides/understanding-ember/the-view-layer/#toc_accessing-template-variables - 從視角) – MrVinz