How to force binding re-evaluate or re-rendering in Aurelia

后端 未结 1 1539
广开言路
广开言路 2021-02-05 18:45

I am starting with a simple TODO app with Aurelia, RethinkDB & Socket.IO. I seem to have problem with re-rendering or re-evaluating an object that is changed through Socket.

相关标签:
1条回答
  • 2021-02-05 19:31

    Aurelia observes changes to the contents of an array by overriding the array's mutator methods (push, pop, splice, shift, etc). This works well for most use-cases and performs really well (no dirty-checking, extremely lightweight in terms of memory and cpu). Unfortunately this leaves one way of mutating an array that aurelia can't "see": indexed assignment... eg myArray[6] = 'foo'. Since no array methods were called, the binding system doesn't know the array changed.

    In your case, try changing this:

    // update item
    socket.on("todo_update", data => {
      let pos = arrayFindObjectIndex(this.items, 'id', data.id);
      if(pos >= 0) {
        console.log('before update');
        console.log(this.items[pos]);
    
        this.items[pos] = data; // <-- change this to: this.items.splice(pos, 1, data);
    
        this.items[pos].title = this.items[pos].title + ' [updated]';
        console.log('after update');
        console.log(this.items[pos]);
      }
    });
    
    0 讨论(0)
提交回复
热议问题