2011-12-15 51 views
0

我第一次使用Knockoutjs,並且因爲無法在控制檯中記錄變量而無法調試。我可以看到我的JS在控制檯中正確加載,當我輸入時:如何將`console.log`與knockout結合使用並訂閱Knockoutjs?

Home.TwitterFeedComponent我看到一個object返回。我如何將console.log與knockout和訂閱結合使用?

var Home = Home || {}; 

var inheriting = inheriting || {}; 

Home.TwitterFeedComponent = function(attributes) { 
    if (arguments[0] === inheriting) 
    return; 
    Home.OnScreenComponent.call(this, attributes); 

    var component = this; 
    var recent_tweets = ko.observableArray(); 
    var url = 'https://twitter.com/search.json?callback=?'; 

    this.attributes.twitter_user_handle.subscribe(function(value) { 

    var twitter_parameters = { 
     include_entities: true, 
     include_rts: true, 
     from: value, 
     q: value, 
     count: '3' 
    } 

    result = function getTweets(){ 
     $.getJSON(url,twitter_parameters, 
     function(json) { 
      console.log(json) 
     }); 
    } 

    console.log(twitter_parameters); 

}); 
}; 

Home.TwitterFeedComponent.prototype = new Home.OnScreenComponent(inheriting); 
Home.TwitterFeedComponent.prototype.constructor = Home.TwitterFeedComponent; 
+0

你的問題不清楚。你是什​​麼意思,*「將console.log與knockout和subscribe一起使用?」* – 2011-12-15 19:36:32

回答

1

我沒有看到這個問題在你的代碼,但如果你想登錄「觀測量」,你有如下記錄它:

console.log(observableVar()); 
+0

無論如何要從HTML中記錄一個值? - 假設我們有一個`foreach`,並且我們想記錄`item` ...?是否有'data-bind =「調用:someFn(item)」```function someFn(item){return function(){console.log(item)}}`或者`data-bind =「text: console.log(item)「`(ps。< - 將不起作用;))。 – Cody 2014-06-12 15:48:38

-1

我有點不清楚到問題的確切範圍 - 但是,如果像我一樣,這個問題是針對在你的HTML中使用console.log

這裏是一個小組代碼,可以幫助:

<div class="tab-content" data-bind="with: ClientSearch.selectedClient"> 

... 

<table class="table table-striped table-condensed table-hover"> 
    <thead></thead> 
    <tbody> 
     <!-- ko foreach: { data: _general, as: 'item' } --> 
     <tr> 
      <td data-bind="text: eval('console.log(\' le item \', item)')"></td> 
     </tr> 
     <!-- /ko --> 
    </tbody> 
</table> 

... 

</div> 

此代碼只需登錄一個foreach到控制檯內的項目。

希望這會有所幫助!

相關問題