如何将Route属性绑定到Controller,以便路由属性更改将反映在控制器中

时间:2016-09-12 16:25:09

标签: ember.js

路由中更改isOpen属性应该反映在我的组件中。我们怎样才能做到这一点?。

Ember-Twiddle

路由/ application.js中

import Ember from 'ember';

export default Ember.Route.extend({
  isOpen:true,
  setupController(controller,model){
    this._super(...arguments);
    controller.set('isOpen',this.get('isOpen')); //whenever i set route property, will this update controller?
    console.log('setupController');
  },
  actions:{
    toggleOpen(){
      console.log('before toggle - ',this.get('isOpen'));
      this.toggleProperty('isOpen'); //changing this route property is not reflected in controller.
      //this.controller.toggleProperty('isOpen'); //changing controller is reflecting.
      console.log('after toggle - ',this.get('isOpen'));
    }
  }
});

控制器/ application.js中

import Ember from 'ember';

export default Ember.Controller.extend({
  appName: 'Ember Twiddle'
});

模板/ application.hbs

<h1>Welcome to {{appName}}</h1>
<br>
{{isOpen}}
{{my-component isOpen=isOpen toggleOpen='toggleOpen'}}
<br>
{{outlet}}
<br>
<br>

模板/组件/我的-component.hbs

my-component {{isOpen}}
<button {{action 'toggleOpen'}}> Toggle </button>
{{yield}}

组件/我的-component.js

import Ember from 'ember';

export default Ember.Component.extend({
  actions:{
    toggleOpen(){
      this.sendAction('toggleOpen');
    }
  }
});

1 个答案:

答案 0 :(得分:2)

因为charprimitive typed属性。不管它是不是一个物体。 Sample Twiddle

more simplistic one