2017-02-07 14 views
0

我在Aurelia中的View中有一個元素,它在Viewmodel中的對象正在更新時未得到更新。我已經看到了關於Pub/Sub和Event Aggregators的文檔,但是對於我想要做的事情,這看起來有點過分,因爲我沒有試圖在兩個不同的資源之間進行通信,而只是在View和Viewmodel中進行通信。字符串插值在更改爲Aurelia中的Viewmodel中的對象後未得到更新

當Viewmodel中的對象發生更改時,我不知道如何正確更新(或觸發更新)視圖中的字符串插值。

我的代碼如下

myview.html

<h1>My List</h1> 
<ul> 
    <li repeat.for="group of modelObject.groups"> 
     <span>${group.id}</span> 
     <span repeat.for="state of group.region">${state}</span> 
    </li> 
<ul> 
<button click.delegate(editModelObject())>Edit</button> 

myviewmodel.js

constructor() 
{ 
    this.modelObject = { 
     <other stuff>, 
     "groups": [ 
      { 
       "id": "default", 
       "regions" : ["NY", "CT", "NJ"] 
      }, 
      { 
       "id": "west", 
       "regions" : ["CA", "OR"] 
      } 
     ], 
     <more stuff> 
    } 
} 

editModelObject() { 
    <something that updates both the id and regions of a group in this.modelObject> 
} 

出於某種原因,狀態被正確地在視場改變,但是ID不是。我是否需要使用類似Pub/Sub的內容來使雙向綁定正常工作?還是有一件簡單的事情,我錯過了或做錯了?

回答

1

如果更改了某個數組的對象的屬性,這將起作用。但是如果你指定了一個數組的索引,這是行不通的,因爲這需要髒檢查。請參閱https://github.com/aurelia/binding/issues/64

要解決您的問題,您應該使用splice()而不是索引分配。例如:

const newItem = { id: 77, name: 'Test 77', obj: { name: 'Sub Name 77' } }; 
//instead of this.model.items[0] = newItem; use below 
this.model.items.splice(0, 1, newItem); 

運行例如https://gist.run/?id=087bc928de6532784eaf834eb918cffa

相關問題