4

我是淘汰赛的新手,我正在尝试获取计算属性的值,但我得到的只是函数体。我的视图模型是

    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);
};

但是我没有得到连接的字符串

    函数可观察(){
    if (arguments.length > 0) {
    // 写

    // 如果值没有改变,则忽略写入
    if ((!observable['equalityComparer']) || !observable['equalityComparer'](_latestValue, arguments[0])) {
    observable.valueWillMutate();
    _latestValue = 参数[0];
    if (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);
    };

应用视图模型 /

 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对象最后将具有可观察的属性,您可以将其再次包装成一组新的可观察属性......

所以你需要在创建时解开你的 observables 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 回答