Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get property of parent View in Ember?

Tags:

ember.js

This is probably beyond easy, but I'm having a hard time to figure out how to access properties of parent views:

App.ParentView = Ember.View.extend({
  foo: 'bar',

  child_view: Ember.View.extend({

    init: function(){
       // get the value of App.ParentView.foo
       //  ???
    }
  })

});
like image 827
Panagiotis Panagi Avatar asked Feb 14 '12 15:02

Panagiotis Panagi


3 Answers

To get the view: this.get('parentView')

To get the value of foo this.get('parentView.foo')

like image 90
Tom Whatmore Avatar answered Nov 11 '22 13:11

Tom Whatmore


In Ember before 1.0.pre you could also use getPath method, instead of chains of get(), for more succinct (and generally safer) code:

this.getPath("parentView.foo");

What's cool in Ember 1.0.pre is that get() method now supports paths, so you can write

this.get("parentView.foo");
like image 38
tokarev Avatar answered Nov 11 '22 11:11

tokarev


Tom is correct. I also created a JS Fiddle to demonstrate this and also illustrate the special contentView property, which can be useful in this type of situation: http://jsfiddle.net/rSLQK/2/

like image 3
Luke Melia Avatar answered Nov 11 '22 12:11

Luke Melia