How to setup Ember like computed properties in Immutablejs and Redux and Flux and React

前端 未结 4 472
旧巷少年郎
旧巷少年郎 2021-01-05 01:11

I am used to computed properties in Ember Object Model. It\'s a convenient way to specify computed properties that depend on other properties.

Say fullName

4条回答
  •  萌比男神i
    2021-01-05 02:02

    What about something like this?

    export const getWidgetsWithComputedProps = widgets => {
      return widgets.map(w => getWidgetWithComputedProps(w));
    };
    
    export const selectWidgetType = widget => {
      switch (widget.type) {
        case 'line':
          return 'time-series';
        case 'pie':
        case 'bar':
          return 'cross-sectional';
        default:
          console.warn('Back up: that type of widget does not exist!', widget.type);
          return null;
      }
    };
    
    export const getWidgetWithComputedProps = createSelector(
      widget => widget,
      selectWidgetType,
      (widget, _type) => {
        return {...widget, _type}
      }
    );
    

提交回复
热议问题