WebDevDude
WebDevDude

Reputation: 859

EmberJS Sending a property to another controller

I'm trying to pass back a property to use in a view on another route. For exmaple, my contact/user/new controller is doing the following:

var newContact = this.get('store').createRecord('contact', contactData);
  newContact.save().then((contact) => {
    this.transitionToRoute('contacts');
    this.set('successMessage', 'You have successfully created a new contact!');
  })
  .catch((response) => {
    response.errors.forEach((error) => {
      this.set('errorMessages', error.message);
    });
  });

Everything works for me except that successMessage line.

this.set('successMessage', 'You have successfully created a new contact!');

I'd like to have it accessible on the customers template so I could do something like:

{{#if successMessage}}
  <div class="success-message">
    {{successMessage}}
  </div>
{{/if}}

If anyone has any ideas, that would be great.

Upvotes: 1

Views: 88

Answers (1)

Pascal Boutin
Pascal Boutin

Reputation: 1264

You can declare dependencies between controllers using the needs attribute.

So you have to add this in your controller where you create your record :

needs: 'customers'   // Assuming you have a CustomersController somewhere

And you would set the message in your desired controller by doing :

this.set('controllers.customers.successMessage', 'Fancy message');

Upvotes: 1

Related Questions