1

나는 knockoutjs에 익숙하지 않다. 나는 너에게 기본적인 질문을 가지고있다 :KO 관찰 가능 어레이를 로그 할 수 있습니까?

화면 트위터 핸들을 변경하는 사용자를 성공적으로 구독하고 성공적으로 트윗을 가져 와서 최근 트윗을 표시한다. console.log(json.results[0].text);을 사용하는 사용자 그러나 json.results을 최근 짹짹로 밀어 넣으면 관찰 가능한 어레이가 작동하고 있다고 확신하지 못합니다. recent_tweets.push(json.results[0].text)[] 빈 배열이 나타납니다.

무슨 일입니까? ko.observable 배열을 로깅 할 수 있습니까?

console.log("TwitterFeedComponent loaded") 
TwitterFeedComponent = function(attributes) { 
    if (arguments[0] === inheriting) 
    return; 

    console.log("TwitterFeedComponent() loaded") 

    var component = this; 
    var url = 'https://twitter.com/search.json?callback=?'; 

    this.attributes.twitter_user_handle.subscribe(function(value) { 
    alert("the new value of the twitter handle is " + value); 
    console.log("I have loaded") 

    var url = 'https://twitter.com/search.json?callback=?'; 
    var twitter_parameters = { 
     include_entities: true, 
     include_rts: true, 
     q: 'from:' + value, 
     count: '3' 
    } 

    $.getJSON(url,twitter_parameters, 
    function(json) { 
     result = json.results[0].text 
     recent_tweets.push(json.results[0].text); 
     console.log(recent_tweets); 
     console.log(json.results[0].text); 

    }); 

}); 
}; 
+1

'console.log (recent_tweets())'와 같은 기본 배열을 로깅해야합니다. –

답변

4

관찰 가능 항목의 실제 값에 액세스하려면 배열인지 여부에 관계없이 괄호를 포함해야합니다. 예를 들면 다음과 같습니다 :

var recent_tweets= ko.observableArray(["hello", "hi", "how are you"]); 
console.log(recent_tweets()); 

변수를 할당 할 때도 마찬가지입니다. 여기

는 일반 스칼라 값의 예는 다음과 같습니다

var myObservableName = ko.observable("Luis"); 
myObservableName("Dany"); // changes the name to: Dany 
var Name = myObservableName(); // gets the value and assigns it to the variable Name (in this case the value is "Dany") 
1

다르게이 조금 대답하기 위해, 당신은 항상 마네의 가입() 기능을 사용할 수 있습니다. 다음과 같이의이 속성은 텍스트 필드에 바인딩되어 가정하자, 데모를 위해서

App.MyViewModel = function() { 
    var self = this; 

    self.TestProperty = ko.observable(null); 
} 

: 이제 다음 뷰 - 모델을 가정 해 봅시다

<input type="text" id="TestPropertyField" data-bind="textInput: TestProperty" /> 

을 이제 당신이 원하는 것을 가정하자 이 값이 변경 될 때마다 기록하십시오. 이렇게하려면 다음과 같이 뷰 모델을 업데이트하기 만하면됩니다.

App.MyViewModel = function() { 
    var self = this; 

    self.TestProperty = ko.observable(null); 
    self.TestProperty.subscribe(function(newValue){ 
     console.log("The new value is: " + newValue); 
    }); 
} 
관련 문제