Controller needs


#1

Hello everyone,

I have this controller.

App.ContactsController = Ember.ArrayController.extend({
  isShowNew: false
})

I want to access isShowNew from another controller, so I thought I could use needs property in another controller.

App.ContactsNewController = Ember.Controller.extend({
  needs: "contacts",
  actions: {
    create: function() {
      console.log(this.controllers) 
      console.log(this.get('controllers').contacts) //undefined
    }
  }
});

But all I’m getting is undefined?


#2

This worked.

this.get('controllers.contacts')

#3

Why do you need access to contacts controller?


#4

Because in “ContactsController” i have attribute isVisible I want to set to false so I can hide the “contactsNewView” from “contactsView”.


#5

You could bubble up the action instead.


App.ContactsController = Ember.Controller.extend({
  actions: {
    create: function() {
      this.set('isShowNew', false);
    }
  }
});

App.ContactsNewController = Ember.Controller.extend({
  actions: {
    create: function() {
      // do your stuff here
      return true; // will bubble this action up to the controller above
    }
  }
});

Also, keep in mind that you’d probably want to handle persisting of the created contact in the router instead of the controller.


#6

Well I tried to handle create in the ContactsController in the first place but it doesn’t work :frowning:

I removed the create action from App.ContactsNewController, but the action doesn’t bubble for some reason.

Uncaught Error: Nothing handled the action ‘create’. If you did handle the action, this error can be caused by returning true from an action handler in a controller, causing the action to bubble.

App.ContactsController = Ember.ArrayController.extend({
  actions: {
    create: function() {
      console.log('contactS')
    }
  }
});

App.ContactController = Ember.ObjectController.extend({
  actions: {
    create: function() {
      console.log('contact')
    }
  }
});

Just in case you want to know how I dispatched the action is through

App.ContactsNewView = Ember.View.extend({
  submit: function(evt) {
    evt.preventDefault();
    this.get('controller').send('create')
    return false;
  }
})

#7

Right, my mistake, from controller it will bubble to the closest route and then up the routes.


#8

Actually it’s not your mistake :smiley:

Something is wrong since it never bubbles to the Route.

I would rather put all the actions in the Route instead of using every single controller to handle it.

http://embersherpa.com/articles/crud-example-app-without-ember-data

He is using version 1.0, and he can get the actions to go up to the ApplicationRoute.

Weird!

<form>
  <label>State</label>
  {{input value=state type="text"}}
  <br />

  <button {{action 'create'}}>Create</button>
</form>

#9

This is my app inspector

This is HIS app inspector


#10

I figure it out :smiley:

I just need to use the INDEX view :slight_smile:


#11

Ember Sherpa is me BTW.


#12

This is funny :smiley:

Talking to the same guy who made the sample I’m looking at to create my application :smiley: and you are trying to help me 2 hehe.

Thank you :smiley:


#13

You’re welcome and welcome to Ember community :wink: