以下の内容はhttps://anton0825.hatenablog.com/entry/2015/06/28/000000より取得しました。


knockoutでcomputedプロパティの値を変更した時の処理を書く

writeプロパティにfunctionを渡すと変更時の処理を書ける。以下の例はpureComputedだけどcomputedも同様。

this.fullName = ko.pureComputed({
    read: function () {
        return this.firstName() + " " + this.lastName();
    },
    write: function (value) {
        var lastSpacePos = value.lastIndexOf(" ");
        if (lastSpacePos > 0) { // Ignore values with no space character
            this.firstName(value.substring(0, lastSpacePos)); // Update "firstName"
            this.lastName(value.substring(lastSpacePos + 1)); // Update "lastName"
        }
    },
    owner: this
});

参考:

http://knockoutjs.com/documentation/computed-writable.html




以上の内容はhttps://anton0825.hatenablog.com/entry/2015/06/28/000000より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14