简体   繁体   English

emberjs组件属性没有更新?

[英]emberjs component property not updated?

i have two components in my template: 我的模板中有两个组件:

{{ property-pie-chart 
    models=model.hosts
    defaultProp=""
    filterByDate=filterByDate
    chartData=[]    
}}

{{ paged-filtered-list 
   data=model.hosts
   dates=model.dates 
   page=page 
   pageSize=pageSize
   filterByDate=filterByDate
   pagerView=pagerView
   initRouteAction=( action 'dateInit' )
   dateFilterAction=( action 'filterByDate' )
   termFilterAction=(action 'filterByTerm')    
   sortOrder=sortOrder
   sortField=sortField
}}

I send action from paged-filtered-list component to controller, which triggers route transition with filterByDate as parameter: 我将paged-filtered-list组件的动作发送到控制器,它以filterByDate作为参数触发路由转换:

import Ember from 'ember';

export default Ember.Controller.extend({
    queryParams: [
        'page',
        'pageSize',
        'sortField',
        'sortOrder',
        'filterByDate',
        'filterByTerm'
    ],
    filterByDate: "",
    filterByTerm: "",
    page: 1,
    pageSize: 10,
    pagerView: 4,
    sortField: "",
    sortOrder: 'asc',
    lala: "",
    actions: {
        dateInit: function(sortedDates) {
            if (!this.get('filterByDate')) {
                let params = {
                    filterByDate: sortedDates.get('firstObject').get('key'),
                    page: this.get('page'),
                    pageSize: this.get('pageSize'),
                    pagerView: this.get('pagerView')
                };

                this.transitionToRoute('hosts', { queryParams: params});
            }
        },
        filterByDate: function(value) {
            if (value) {
                let params = {
                    filterByDate: value,
                    page: 1,
                    pageSize: this.get('pageSize')
                };

                this.transitionToRoute('hosts', { queryParams: params});
            }
        },

        filterByTerm: function(value) {
            let params = {
                filterByDate: this.get('filterByDate'),
                page: 1,
                pageSize: this.get('pageSize')
            };

            if (value) {
                params['filterByTerm'] = value;
            } else {
                params['filterByTerm'] = "";
            }

            this.transitionToRoute('hosts', { queryParams: params});
        }
    }
});

Problem is that URL is updated and contains filterByDate, but first component property-pie-chart does not detect that filterByDate property is changed, altough i checked attributes in init/didUpdate methods and parameter is changed, can somebody help and explain what i am doing wrong? 问题是URL已更新并包含filterByDate,但第一个组件属性 - 饼图未检测到filterByDate属性已更改,尽管我检查了init / didUpdate方法中的属性并且参数已更改,有人可以帮助并解释我在做什么错误?

Currently you are not setting filterByDate property in controller. 目前您没有在控制器中设置filterByDate属性。

I would suggest the following approach, You please declare the below property in corresponding route.js , 我建议采用以下方法,请在相应的route.js声明以下属性,

queryParams: { page: { refreshModel: true }, pageSize: { refreshModel: true },sortOrder: { refreshModel: true },filterByDate: { refreshModel: true },filterByTerm: { refreshModel: true }}

refreshModel denotes is whenever this property changed,then it will force to refresh the page. refreshModel表示只要此属性发生更改,它就会强制刷新页面。

and in controller.js , You don't need to call this.transitionToRoute('hosts', { queryParams: params}); controller.js ,您不需要调用this.transitionToRoute('hosts', { queryParams: params}); instead you just set required queryParams participating property alone then transition will automatically taken care. 相反,您只需设置所需的queryParams参与属性,然后转换将自动处理。

SideNote: It's good if you can change function name filterByTerm filterByDate by the way this is not related to problem SideNote:如果您可以通过与问题无关的方式更改函数名称filterByTerm filterByDatefilterByTerm filterByDate

Update: I am glad you sorted out the problem. 更新:我很高兴你解决了这个问题。 but then I want to emphasize what are Computed Properties from ember guides. 但后来我想强调什么是ember指南的计算属性。

In a nutshell, computed properties let you declare functions as properties. 简而言之,计算属性允许您将函数声明为属性。 You create one by defining a computed property as a function, which Ember will automatically call when you ask for the property . 您可以通过将计算属性定义为函数来创建一个, 当您要求属性时Ember将自动调用该属性 You can then use it the same way you would any normal, static property. 然后,您可以像使用任何普通的静态属性一样使用它。

https://guides.emberjs.com/v2.8.0/object-model/computed-properties/#toc_what-are-computed-properties https://guides.emberjs.com/v2.8.0/object-model/computed-properties/#toc_what-are-computed-properties

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

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