2013-03-13 113 views
2

我是新來淘汰賽我試圖得到一個計算屬性的值,但我得到的是函數體。 我的視圖模型是Knockout計算屬性輸出函數體

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

但相反的獲取concatened字符串我得到

 

    function observable() { 
    if (arguments.length > 0) { 
    // Write 

    // Ignore writes if the value hasn't changed 
    if ((!observable['equalityComparer']) || !observable['equalityComparer'](_latestValue, arguments[0])) { 
    observable.valueWillMutate(); 
    _latestValue = arguments[0]; 
    if (DEBUG) observable._latestValue = _latestValue; 
    observable.valueHasMutated(); 
    } 
    return this; // Permits chained assignments 
    } 
    else {..... 

更新抱歉沒有發佈視圖 任何幫助,將不勝感激

更新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只有少數modifcations

+1

你如何獲得價值? – nemesv 2013-03-13 22:34:04

+0

但是如果我在返回之前使用控制檯日誌,我會得到相同的輸出。 – trebor 2013-03-13 22:36:49

+0

你也可以發表你如何填寫'authors'數組?不能說你的'data.firstName'和'data.lastName'已經是ko.observable了嗎?因爲只有在這種情況下,你的代碼才能生成你的輸出... – nemesv 2013-03-13 22:42:48

回答

3

你的問題是在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(), 
}; 
+0

它的工作原理,非常感謝 – trebor 2013-03-13 23:01:49