Referencing components/views from controllers/components

Coming from a Cocoa background, it feels natural to me to be able to have a reference to views and components in a controller.

Let’s say I have a component that has a method someMethod. I want to be able to trigger this from outside of the component (let’s say from a button on the same page). Is it possible to store a reference to the component in the controller, so that I can call this method? Or is this a big Ember anti-pattern?

I could extend this question by asking whether it is possible to reference specific DOM elements from a view or component.

I think having such a possibility opens up a lot of flexibility. Right now the only way I see to achieve such things is to use property bindings or using jQuery to select DOM elements.

The mantra these days is data down actions up. so your component should trigger some method based on a change in the data. If the component wants to change the data it fires an action up for whoever owns the data to handle. Also controllers are going away to be replaced with routable components, think component that a route is tied to. Views are basically deprecated in favor of components. You architect your application as a nested set of components that all fire actions up and observe data that was passed to them. You will be using bindings a lot, you can pretty much avoid jquery most of the time unless you’re integrating a third party lib. As further reinforcement for this architecture in 2.0 bindings will be one way. Hopefully that helps a bit.

Checkout this discussion Communication between components - #5 by ZebraFlesh

The short of it is that it’s not really a fully fleshed out path just yet but there are a number of ways to accomplish what you’re asking.

1 Like

Maybe consider this for now http://www.thesoftwaresimpleton.com/blog/2015/04/27/event-bus/

or more hacky way; If you render each component with a static id e.g {{button id="saveProject"}} you can get access to it via Ember.View.views['saveProject'] and do whatever you want e.g. triggerAction etc

Where is this deprecation of controllers mentioned? Is there a blog post that I missed somewhere?

ObjectController was deprecated in 1.11 Ember.js - Deprecations as proposed in the Ember 2.0 RFC (see Routeable Components, ArrayController is also mentioned in the discussion) and mentioned in the lastest blog post The Transition to Ember 2.0 in Detail