2017-03-13 44 views
1

假設我有一個Aurelia自定義屬性與我一起工作,當綁定到另一個自定義屬性的值更改時是否可以觸發事件?從另一個自定義屬性訪問自定義屬性的綁定

說得更加具體而言,我建立一個奧裏利亞自定義屬性進行選擇的。我得到了所有選擇的工作。

<select>元素上有一個focus.bind="isFocused"。當綁定值發生變化時,我需要知道(在我選擇的自定義屬性中)。

是否有某種方式來訪問焦點(從對焦自定義屬性)結合在我所選擇的自定義屬性?也許使用綁定引擎或甚至內部方法? (注:我考慮製作一個自定義元素來做到這一點,但Aurelia不能很好地包裝元素,我必須爲原始選擇元件上的樣式,類和其他任何東西編寫連接代碼)。

+0

也許你可以使用綁定行爲'signal':http://aurelia.io/hub.html#/doc/article/aurelia/binding/latest/binding-binding-behaviors/5剛第一個想法 –

+0

@marc我深入瞭解了這一點。它不適用於這種情況。 – Vaccano

回答

0

我不確定這是否是正確的方式,但這是迄今爲止我找到的最簡單的方法。

我-attr.js

import { inject } from 'aurelia-framework'; 
import { Focus } from 'aurelia-templating-resources'; 

@inject(Focus) 
export class MyAttrCustomAttribute { 

    constructor(focusAttr) { 
    this.focusAttr = focusAttr; 
    } 

    attached() { 
    console.log(this.focusAttr.value); //bound value 

    //... 
    //extending the change event. BE CAREFUL! 
    //... 

    const originalValueChanged = this.focusAttr.valueChanged; 
    this.focusAttr.valueChanged = function(newValue) { 
     console.log('Focus bound value has changed!'); //<-- call an event before "focus" 
     originalValueChanged.call(this, newValue); //<-- "focus" is enqueued 
     // <--- enqueue an event event here to be called after "focus" 
    } 

    } 
} 

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