Re: [KnockoutJS] Observable array not updating

47 переглядів
Перейти до першого непрочитаного повідомлення

Gunnar Liljas

не прочитано,
21 черв. 2018 р., 18:39:4621.06.18
Кому: knock...@googlegroups.com
The points property used in the computed must be observable.

function Stat(name, initial) {
    var self = this;
    self.name = name;
    self.points = ko.observable(initial.points);

    self.bonus = ko.computed(function() {
        var bonus = self.points() / 2 - 5;
        return bonus;        
    });    
}


Den tors 21 juni 2018 kl 20:19 skrev Siem van den Berg <siemvan...@gmail.com>:
Hello,

Trying to build a simple calculator in knockout to learn some more. The points value is being updated but the computer bonus value is only calculated once and never updates.


<table class="table">
    <thead><tr>
        <th>Stat</th><th>Points</th><th>Points</th><th>Bonus</th>
    </tr></thead>
    <tbody data-bind="foreach: stats">
        <tr>
            <td><span data-bind="text: name" /></span></td>
            <td>
<input type="number" data-bind="value: points, valueUpdate: ['afterkeydown', 'input']"/>
</td>
<td><span data-bind="text: points"></span></td>

            <td data-bind="text: bonus"></td>
        </tr>    
    </tbody>
</table>


function Stat(name, initial) {
    var self = this;
    self.name = name;
    self.stat = ko.observable(initial);
    self.points = ko.observable(self.stat().points);

    self.bonus = ko.computed(function() {
        var bonus = self.stat().points / 2 - 5;
        return bonus;        
    });    
}

function statViewModel() {
    var self = this;

    self.stats = ko.observableArray([
        new Stat("STR", { statName: "STR", points: 10 }),
        new Stat("DEX", { statName: "DEX", points: 12 }),
        new Stat("CON", { statName: "CON", points: 14 }),
        new Stat("INT", { statName: "INT", points: 16 }),
        new Stat("WIS", { statName: "WIS", points: 18 }),
        new Stat("CHA", { statName: "CHA", points: 8 })
    ]);
}

ko.applyBindings(new statViewModel());


--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Відповісти всім
Відповісти автору
Переслати
0 нових повідомлень