admin管理员组文章数量:1295304
In my application I need to have some forms with rows of values that have to be summed. I need to loop through these rows, having inputs for them and then building a sum that should be updated when the inputs are edited.
Here is a simplified example: The class:
export class example {
items = [
{ id: 1, val: 100 },
{ id: 2, val: 200 },
{ id: 3, val: 400 }
];
get sum() {
let sum = 0;
for (let item of this.items) {
sum = sum + parseFloat(item.val);
}
return sum;
}
}
The view:
<div repeat.for="item of items" class="form-group">
<label>Item ${$index}</label>
<input type="text" value.bind="item.val" class="form-control" style="width: 250px;">
</div>
<div class="form-group">
<label>Summe</label>
<input type="text" disabled value.one-way="sum" class="form-control" style="width: 250px;" />
</div>
Until here everything is working like I expect it to do. But: it's dirty checking on sum
all the time and I fear running into performance issues in the more plicated app. So I tried to use the @putedFrom
decorator, but none of these versions works:
@putedFrom('items')
@putedFrom('items[0]', 'items[1]', 'items[3]')
@putedFrom('items[0].val', 'items[1].val', 'items[3].val')
In all of these cases, the sum is just calculated once but not after editing the values. And the last 2 would be no good solution, because I can have a changing amount of items in my model.
Any suggestions how I could get a puted value that is changed when fields which it depends on are changed without having the dirty checking?
In my application I need to have some forms with rows of values that have to be summed. I need to loop through these rows, having inputs for them and then building a sum that should be updated when the inputs are edited.
Here is a simplified example: The class:
export class example {
items = [
{ id: 1, val: 100 },
{ id: 2, val: 200 },
{ id: 3, val: 400 }
];
get sum() {
let sum = 0;
for (let item of this.items) {
sum = sum + parseFloat(item.val);
}
return sum;
}
}
The view:
<div repeat.for="item of items" class="form-group">
<label>Item ${$index}</label>
<input type="text" value.bind="item.val" class="form-control" style="width: 250px;">
</div>
<div class="form-group">
<label>Summe</label>
<input type="text" disabled value.one-way="sum" class="form-control" style="width: 250px;" />
</div>
Until here everything is working like I expect it to do. But: it's dirty checking on sum
all the time and I fear running into performance issues in the more plicated app. So I tried to use the @putedFrom
decorator, but none of these versions works:
@putedFrom('items')
@putedFrom('items[0]', 'items[1]', 'items[3]')
@putedFrom('items[0].val', 'items[1].val', 'items[3].val')
In all of these cases, the sum is just calculated once but not after editing the values. And the last 2 would be no good solution, because I can have a changing amount of items in my model.
Any suggestions how I could get a puted value that is changed when fields which it depends on are changed without having the dirty checking?
Share edited Oct 12, 2016 at 23:00 Jeremy Danyow 26.4k12 gold badges90 silver badges135 bronze badges asked Aug 14, 2015 at 12:25 doeckdoeck 2652 silver badges14 bronze badges1 Answer
Reset to default 9use @putedFrom
on the item val
prop:
import {putedFrom} from 'aurelia-framework';
export class Item {
constructor(id, val, parent) {
this.id = id;
this._val = val;
this.parent = parent;
}
@putedFrom('_val')
get val() {
return this._val;
}
set val(newValue) {
this._val = newValue;
this.parent.calculateSum();
}
}
export class Example {
sum = 0;
items = [
new Item(1, 100, this),
new Item(2, 200, this),
new Item(3, 400, this)
];
constructor() {
this.calculateSum();
}
calculateSum() {
let sum = 0;
for (let item of this.items) {
sum = sum + parseFloat(item.val);
}
this.sum = sum;
}
}
For some other ideas, take a look at https://stackoverflow./a/32019971/725866
本文标签: javascriptAurelia Binding to computed valuesStack Overflow
版权声明:本文标题:javascript - Aurelia: Binding to computed values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741613978a2388421.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论