4

i have search property in ApplicationController and its linked with input field of searching. i want to access search field of ApplicationController in ProjectController. it should be sync. i use following code but its not working.

/ app/controllers/projects/index.js (project Controller)

import Ember from 'ember';
export default Ember.Controller.extend({
    needs: ['application'],
    searchBinding: 'controllers.application.search'
});

/ app/controllers/application.js (Application Controller)

  import Ember from 'ember';
    export default Ember.Controller.extend({
      search: ''
    )}

application.hbs

{{input value = search}}

Muhammad Ateek
  • 1,057
  • 3
  • 14
  • 24
  • What ember version do you use? `needs` and bindings are deprecated in current ember versions. – Lux May 20 '16 at 11:16

3 Answers3

15

Ember needs is deprecated and is now used differently.

It works like this:

applicationController: Ember.inject.controller('application'),
mySearch: Ember.computed.alias('applicationController.search')

In your hbs template -

{{mySearch}}

 is in sync with applications property "search".

kristjan reinhold
  • 2,038
  • 1
  • 17
  • 34
6

You can access any controller within controller by inject it.

import Ember from 'ember';
export default Ember.Controller.extend({
    applicationController: Ember.inject.controller('application'),
    searchProperty: Ember.computed.alias('applicationController.search'),
)};

Managing Dependences between Ember controllers

Muhammad Ateek
  • 1,057
  • 3
  • 14
  • 24
  • 2
    Now it's `import { inject } from '@ember/controller'; /*...*/ applicationController: inject('application')` – crusy Feb 09 '18 at 07:23
1

You can access controllers properties included with needs this way :

{{controllers.neededController.property}}

In you case try :

{{input value=controllers.application.search}}

See example here : http://jsfiddle.net/6Evrq/

dynamic_cast
  • 1,075
  • 1
  • 9
  • 23
  • You are right, the method I show is deprecated. I added a jsfiddle example in case someone needed a solution for an older version of Ember but your answer is definitely better. – dynamic_cast May 22 '16 at 15:01