使用Aurelia's ref属性引用元素的视图模型
Referencing view-model of element using Aurelia's ref attribute
Aurelia提供了ref
属性,该属性被认为能够提供对自定义元素视图模型的引用。
我正在尝试构建一个示例,其中一个文本框(生产者)的值管道到另一个只读文本框(消费者)。
按预期:
- 在生产者文本框中输入
producerInput
,设置新值。 -
producerOutput
被反复轮询,因为与consumerInput
的绑定需要更新。 -
consumerOutput
被反复轮询,因为绑定到消费者文本框应该更新。
然而consumerInput
没有被调用。如果我理解正确的话,它应该在每次调用producerOutput
之后被调用。没有调用consumerInput
意味着该值没有通过管道传递到消费者文本框。为什么不调用consumerInput
,我如何修复它?
ref.html
<template>
<require from="producer"></require>
<require from="consumer"></require>
<producer producer.ref="producerViewModel"></producer>
<consumer consumerInput.bind="producerViewModel.producerOutput"></consumer>
</template>
ref.js
export class Ref {
}
producer.html
<template>
<div>
producer: <input value.bind="producerInput" />
</div>
</template>
producer.js
import {customElement} from 'aurelia-framework';
@customElement('producer')
export class Producer {
set producerInput(v) {
this.value = v;
}
get producerOutput() {
return this.value;
}
}
consumer.html
<template>
<div>
consumer: <input value.bind="consumerOutput" readonly />
</div>
</template>
consumer.js
import {customElement, bindable} from 'aurelia-framework';
@customElement('consumer')
@bindable('consumerInput')
export class Consumer {
set consumerInput(v) {
this.value = v;
}
get consumerOutput() {
return this.value;
}
}
更新后的工作方案:
ref.html:
<template>
<require from="producer"></require>
<require from="consumer"></require>
<producer producer.ref="producerViewModel"></producer>
<consumer input.bind="producerViewModel.output"></consumer>
</template>
ref.js
export class Ref {
}
producer.html
<template>
<div>
producer: <input value.bind="output" />
</div>
</template>
producer.js
import {bindable} from 'aurelia-framework';
export class Producer {
@bindable output;
}
consumer.html
<template>
<div>
consumer: <input value.bind="input" readonly />
</div>
</template>
consumer.js
import {bindable} from 'aurelia-framework';
export class Consumer {
@bindable input;
}
DOM自动将所有属性小写。不能使用consumerInput
或producerOutput
作为可绑定的属性名。Aurelia使用一种惯例,将混合大小写的可绑定属性名连字符连接起来。
例如,class MyElement { @bindable fooBar }
可以这样使用:
<my-element foo-bar.bind="baz"></my-element>
试试这个:
import {customElement, bindable} from 'aurelia-framework';
@customElement('consumer')
export class Consumer {
@bindable input;
inputChanged(newValue, oldValue) {
// aurelia will call this automatically- a convention looks for methods on the vm that match bindable property names.
}
}
对于生产者自定义元素也需要进行类似的修改。
相关文章:
- 点击具有更改元素引用的变量
- 如何使用javascript通过id获取元素引用的数量
- 将jQuery元素引用作为对象属性进行访问
- UIWebView和JavaScript:存储元素引用,然后稍后使用
- 将jquery元素引用传递给vanilla javascript
- 将DOM元素引用从DOM中移除时更改为null引用
- 管理javascript项目中的元素引用
- AngularJS指令中的元素引用会导致内存泄漏吗?
- Webdriverjs没有休眠语句,陈旧的元素引用
- 没有事件在谷歌Chrome从SVG元素引用的使用标签
- 确定哪个元素引用了当前html
- Javascript构建元素引用节点列表
- 为DOM元素引用创建常数时间数据结构
- 为什么我不能将这个元素引用设置为一个变量?
- Backbonejs:将parent's元素引用传递给子视图是一种好的做法
- 如何在拼接/排序数组时保持jQuery DOM元素引用?
- 将元素引用传递给指令
- 使元素引用不那么笨拙
- 元素引用在修改容器的内部 HTML 属性时中断
- 在Knockout JS中保存对DOM元素引用的正确模式