2012-11-03 23 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