将对象转换为地图时,简单视图不会使用Aurelia更新

时间:2016-03-21 23:57:15

标签: javascript aurelia

我创建了一个要点来说明我的问题:

https://gist.run/?id=8d03b3b8f8deef92e3a9

相关的ViewModel:

import {inject} from 'aurelia-framework';
import obj from 'otherModule';

@inject(obj)
export class Example {

  constructor(obj) {
    this.obj = obj;
    this.index = Object.keys(obj).length;
  }

  addAnother() {
    this.obj[ this.index ] = `test${this.index}`;
    this.index++;
  }
}

查看

<template>
  <require from="./asMap.js"></require>
  <ul>
    <li repeat.for="[key, value] of obj | asMap">${value}</li>
  </ul>

  <button click.trigger="addAnother()">Add to Object</button>
</template>

我的问题摘要涉及尝试从视图中的简单对象显示内容。我想在视图中使用Aurelia的repeat运算符,并使用Map()语法来执行此操作(例如repeat.for="[key, value] of object")。出于这个原因,我通过valueConvert模块将对象转换为Map:

export class asMapValueConverter {
    toView(object) {
        return Object.keys(object).reduce((map, current) => {
            map.set(current, object[ current ]);
            return map;
        }, new Map());
    }

}

这可以在视图初始化时显示数据,但如果在ViewModel中更新了对象,则它不会在视图中更新。

此时如果我导航到另一个视图并返回,它将显示添加的元素。据我所知,没有办法更新视图而不导航到另一个并返回。

这是一个错误吗?我只是在做一些可怕的事吗?任何解决方法?

1 个答案:

答案 0 :(得分:0)

来自:https://github.com/aurelia/binding/issues/356

这是您的选择。只有在提前定义了所有对象的道具时,它才会起作用: https://gist.run/?id=b9071aaa4447426a0570

唯一的另一种方法是使用脏检查,因为没有其他方法可以判断何时在没有Object.observe的情况下添加/删除属性。

相关问题