简体   繁体   English

Ember.js设置控制器属性表单组件

[英]Ember.js set controller property form component

I have a custom component called {{search-box}} , now on every keyUp() event I get the input value using this.get('query') now in order to reload my model with this specific search query, I have setup my search controller like this: 我有一个名为{{search-box}}的自定义组件,现在在每个keyUp()事件上,我都使用this.get('query')获取输入值,以便使用此特定搜索查询重新加载模型,像这样设置我的search控制器:

import Ember from 'ember';

export default Ember.Controller.extend({

  queryParams: ['q'],
  q: null

});

and my router like this: 和我的路由器是这样的:

import Ember from 'ember';

export default Ember.Route.extend({

  queryParams: {
    q: { refreshModel: true }
  },

  model: function(params){
    // call store here
  }

});

everything works fine except setting the controller.q property from inside the component. 除了从组件内部设置controller.q属性之外,其他所有方法都工作正常。 I have tried using this.sendAction() from the component hoping to catch it inside the controller and set the property this way but was unsuccessful. 我尝试从组件中使用this.sendAction() ,希望将其捕获到控制器内并以这种方式设置属性,但未成功。 Any ideas on how to make this work? 关于如何进行这项工作的任何想法?

Components are by default isolated. 默认情况下,组件是隔离的。 So they don't know anything about controllers or other components from the outside. 因此,他们对外部控制器或其他组件一无所知。 The only way a component can change values from the outside is by passing this values to the component (for example when defining the component in your template): 组件可以从外部更改值的唯一方法是将这些值传递给组件(例如,在模板中定义组件时):

// template
{{search-box search=q}}

// component
this.set('search', 'someValue')

Above will set the search property within the component. 上面将在组件内设置搜索属性。 Because the controller's q property is passed as search property, setting the search property in your component will set the q property in your controller (two-way binded) 由于控制器的q属性作为搜索属性传递,因此在组件中设置search属性将在控制器中设置q属性(双向绑定)

This would also work with an action bind to the component: 这也可以与绑定到组件的动作一起使用:

// template
{{search-box searchChanged='updateQuery'}}

// component
this.sendAction('searchChanged', 'someValue')

// controller
actions: {
    updateQuery: function(value) {
        this.set('q', value);
    }
}

Hope this will help you in the right directions. 希望这会在正确的方向为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM