2014-08-28 26 views
0

我有一個父視圖和一個嵌套在父視圖中的子視圖。父視圖和子視圖實例中都有一些變量。Ember - 在父視圖和子視圖之間獲取值

現在我需要獲取parentView和childView之間的值。

我的模板:

<script type="text/x-handlebars" data-template-name="application"> 
    <h2> Welcome to Ember.js</h2> 
    {{view App.PreviewView}} 
</script> 

父視圖:

<script type="text/x-handlebars" data-template-name="previewdiv"> 
    <div> Preview Div </div> 
    <div {{action 'getChildValue' target='view'}}>Get Child Value </div> 
    {{view App.ChildpreviewView }} 
</script> 

子視圖:

<script type="text/x-handlebars" data-template-name="childpreviewdiv"> 
    <div> Child Preview Div </div> 
    <div {{action 'getParentValue' target='view'}}>Get Parent Value </div> 
</script> 

App.js:

App.PreviewView = Ember.View.extend({ 
    templateName: 'previewdiv', 
    tempParentValue: true, 
    actions: { 
     getChildValue: function(){ 
      // How to get "tempChildValue" here. 
     } 
    } 
}); 

App.ChildpreviewView = Ember.View.extend({ 
    templateName: 'childpreviewdiv', 
    tempChildValue: true, 
    actions: { 
     getParentValue: function() { 
      // How to get "tempParentValue" here. 
     } 
    } 
}); 

JSBIN Link

回答

0

編輯您的JSBIN

http://jsbin.com/wihowu/2/edit

要訪問parentview您只需鍵入:

this.get('parentView'); 

要訪問所有childviews:

this._childViews 

你可以訪問的,才需要childVie w

var self = this; 
this._childViews.forEach(function(childView){ 
    if(childView.get("templateName") === "childpreviewdiv") { 
     self.set('wantedChildView', childView); 
    } 
}); 
+0

感謝您的回覆。在我們的開發代碼中使用「_childViews」(下劃線方法)是否是好的做法,因爲通常在框架代碼中使用了「_methods」。通常在開發中使用「_methods」是不可取的。如果我錯了,請糾正我。 – Jeevi 2014-08-28 08:04:15

+0

@Jeevi沒關係 – 2014-08-28 11:52:39