4

私は計算されたプロパティの値を取得しようとしていますが、取得するのは関数本体だけです。私のViewModelは

    var AuthorViewModel = function (data) {
    this.id        = ko.observable(data.id);
    this.firstName = ko.observable(data.firstName);
    this.lastName  = ko.observable(data.lastName);
    this.email     = ko.observable(data.email);
    this.phone     = ko.observable(data.phone)
    this.address   = ko.observable(data.address);
    this.fullName = ko.computed(function () {
        return this.firstName() + " " + this.lastName();
    },this);
};

しかし、連結された文字列を取得する代わりに、取得します

    関数 observable() {
    if (arguments.length > 0) {
    // 書く

    // 値が変更されていない場合、書き込みを無視します
    if ((!observable['equalityComparer']) || !observable['equalityComparer'](_latestValue, 引数[0])) {
    observable.valueWillMutate();
    _latestValue = 引数[0];
    (DEBUG) observable._latestValue = _latestValue の場合。
    observable.valueHasMutated();
    }
    これを返します。// 連鎖代入を許可します
    }
    そうしないと {.....

ビューを投稿していないことをお詫びします

<h1>Demo Application</h1>

<ul data-bind="foreach: authors">
    <li>
        <span data-bind="text: fullName()"></span>
        <a href="#" data-bind="click: edit">Edit</a>
    </li>
</ul>

<div>
    <button class="btn" data-bind="click: addAuthor">Add Author</button>
</div>

どんな助けでもいただければ幸いです

更新 2 AuthorsViewModel

   var AddAuthorViewModel = function() {
        this.id = ko.observable();
        this.firstName = ko.observable();
        this.lastName = ko.observable();
        this.email = ko.observable();
        this.phone = ko.observable();
        this.address = ko.observable();
    };

    AddAuthorViewModel.prototype.template = "AddAuthor";

    AddAuthorViewModel.prototype.add = function () {
        this._requireModal();

        var newAuthor = {
            id        : this.id,
            firstName : this.firstName,
            lastName  : this.lastName,
            email     : this.email,
            phone     : this.phone,
            address   : this.address,
        };
        // Close the modal, passing the new author object as the result data.
        this.modal.close(newAuthor);
    };

AppViewModel /

 The root view model for the application
    var AppViewModel = function() {
        this.authors = ko.observableArray();
    };

    AppViewModel.prototype.addAuthor = function() {
        showModal({
            viewModel: new AddAuthorViewModel(),
            context: this // Set context so we don't need to bind the callback function
        }).then(this._addAuthorToAuthors);
    };

    AppViewModel.prototype._addAuthorToAuthors = function (newAuthorData) {
        this.authors.push(new AuthorViewModel(newAuthorData));
    };

私は実際にhttp://aboutcode.net/2012/11/15/twitter-bootstrap-modals-and-knockoutjs.htmlのチュートリアルをいくつか変更して使用しています

4

1 に答える 1

5

あなたの問題はnewAuthor作成部分にあります:

var newAuthor = {
    id        : this.id,
    firstName : this.firstName,
    lastName  : this.lastName,
    email     : this.email,
    phone     : this.phone,
    address   : this.address,
 };

このコードでは、値ではなくオブザーバブル自体を参照するオブジェクトを作成しているためです。

したがってAuthorViewModeldataオブジェクトの最後には、観測可能なプロパティがあり、それを新しい観測可能なプロパティのセットに再度ラップします...

したがって、次を作成するときに、オブザーバブルをアンラップする必要がありますnewAuthor

var newAuthor = {
    id        : this.id(),
    firstName : this.firstName(),
    lastName  : this.lastName(),
    email     : this.email(),
    phone     : this.phone(),
    address   : this.address(),
};
于 2013-03-13T22:54:37.377 に答える