Mobx可观察的属性

时间:2018-10-16 12:45:03

标签: properties observable mobx

通过此操作将可观察的属性添加到数组之后

fun bindDefaultLineChart(chartView: LineChart,
                                 marketValues: List<MarketVal>?,
                                 label: String?) {
            if (marketValues == null) {
                return
            }

            val yValues = ArrayList<Entry>()
            var maxCount: Long = 0
            for (marketValue in marketValues) {
                val entry = Entry(marketValue.dateTime.toFloat(),
                        marketValue.transactionNumber.toFloat())
                yValues.add(entry)
                if (marketValue.transactionNumber > maxCount) {
                    maxCount = marketValue.transactionNumber.toLong()
                }
            }

            val xAxis = chartView.xAxis
            val xAxisFormatter = ChartValueDateFormatter(Constants.DEFAULT_DATE_FORMAT)
            xAxis.valueFormatter = xAxisFormatter

            chartView.axisLeft.apply {
                axisMaximum = (maxCount + MAX_CHAR_Y_AXIS_ADDITION).toFloat()
                valueFormatter = NonFloatValueFormatter()
            }

            val dataSet = LineDataSet(yValues, label).apply {
                axisDependency = YAxis.AxisDependency.LEFT
                color = ContextCompat.getColor(chartView.context, R.color.colorAccent)
                formLineWidth = 2f
                setDrawIcons(true)
                setDrawValues(false)
            }

            val lineData = LineData(dataSet)
            chartView.data = lineData
        }
}

我尝试使用以下方法更改属性的值:

@action
improvePrograms = function() {
    let data = this.dataPro;
    this.dataProImproved = data;
    for (i = 0; i < data.length; i++) {
      extendObservable(this.dataProImproved[i], {
        wishedFor: false,
        inCart: false,
        sold: false
      });
    }
  };

一切似乎都很好,但是即使将属性的值更改为true,使用observable属性更改颜色的组件也不会反应或重新渲染。

  @action
  addToWishList = function(item) {
    for (i = 0; i < this.dataProImproved.length; i++) {
      if (this.dataProImproved[i].program_id === item.program_id && this.dataProImproved[i].wishedFor === false) {
        this.dataProImproved[i] = { ...this.dataProImproved[i], wishedFor: true };
        console.log("Program added to wish list: ", this.dataProImproved[i]);
        this.getWishList();
      }
    }
  };

有人可以向我解释我在这里做错了什么吗?类组件用@observer装饰并注入到商店中。

1 个答案:

答案 0 :(得分:0)

所以我让它通过以下方式传递数据数组:

@computed get getDataProImproved(){return this.dataProImproved}

不要忘记在将数组传递给FlatList之前对其进行切片... 一切都很好...