Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set parent controller property from child controller

Tags:

ember.js

I have a flyers route that has a template called flyers.hbs

<div class="button-wrap">
    <button {{action 'back'}}>Go Back</button>
    {{#if isPrintable}}
        <button {{action 'print'}} class="float-right">Print Flyer</button>
    {{/if}}
</div>

{{outlet}}

In this flyers route I have view and new. New should only show the back button and view should show the back button and the print button. So in the view controller I specified a property like so.

import Ember from 'ember';

export default Ember.Controller.extend({
    isPrintable: true,
});

But obviously the parent controller for flyers does not see that property when I navigate to the view route so my print button is not showing.

What is the proper way to do this?

like image 411
Jordan Avatar asked Apr 16 '26 22:04

Jordan


1 Answers

As I understand you'd like to have {{isPrintable}} in flyers template with value dependent of active child route. Maybe this will work for you.

//flyers controller
import Ember from 'ember';

export default Ember.Controller.extend({
   isPrintable: true,
});

//child route
import Ember from 'ember';
export default Ember.Route.extend({

  parentController: Ember.computed( function() {
    return this.controllerFor('flyers');
  }),

  setupController: function(controller, model) {
    this._super(controller, model);
    this.get('parentController').set('isPrintable', false);
  },

  deactivate: function() {
    this.get('parentController').set('isPrintable', true);
  }
});
like image 73
artych Avatar answered Apr 19 '26 16:04

artych



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!