2012-10-03 2 views
0

를 업데이트하지 : KO로 기능 toJSON을 사용 jsonData라는 변수가 http://jsfiddle.net/Xa9ez/녹아웃 JSON은 다음과 바이올린에서

입니다.

다른 변수가 업데이트 될 때 업데이트되지 않는 이유는 무엇입니까? 또는 goCaps 함수를 호출해도?

스크립트 :

function AppViewModel() { 

    this.firstName = ko.observable("Bert"); 
    this.lastName = ko.observable("Bertington"); 

    this.fullName = ko.computed(function() { 
     return this.firstName() + " " + this.lastName();  
    }, this); 

    this.capitalizeLastName = function() { 
     var currentVal = this.lastName();  // Read the current value 
     this.lastName(currentVal.toUpperCase()); 

     this.data(ko.toJSON(this)); 

    };  
    this.data = ko.observable(ko.toJSON(this)); 
} 

More about observables :

function AppViewModel() { 

    this.firstName = ko.observable("Bert"); 
    this.lastName = ko.observable("Bertington"); 

    this.fullName = ko.computed(function() { 
     return this.firstName() + " " + this.lastName();  
    }, this); 

    this.capitalizeLastName = function() { 
     var currentVal = this.lastName();  // Read the current value 
     this.lastName(currentVal.toUpperCase()); 

     this.data = ko.toJSON(this); 

    };  
    this.data = ko.toJSON(this); 
} 
var appViewModel = new AppViewModel() 
// Activates knockout.js 


ko.applyBindings(appViewModel); 

HTML을

<p>Last name: <strong data-bind="text: lastName"></strong></p> 

<p>First name: <input data-bind="value: firstName" /></p> 
<p>Last name: <input data-bind="value: lastName" /></p> 

<p>Full name: <strong data-bind="text: fullName"></strong></p> 

<button data-bind="click: capitalizeLastName">Go caps</button> 
<p>JSON: <strong data-bind="text: data"></strong></p> 
​ 
+0

'this.data'는 관찰 가능하지 않은 것 같습니다. –

+0

어떻게 관찰 가능하게 만들 수 있습니까? –

답변

1

당신은 this.data이 관찰해야한다.