2011-12-15 41 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到控制台内的项目。

希望这会有所帮助!