最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to log a KO observable array? - Stack Overflow

programmeradmin2浏览0评论

I am new to knockoutjs and I have an uber-basic question for you:

I have been able to successfully subscribe to a user changing the on screen twitter handle AND successfully fetch the tweets and display the last recent tweet of a user using console.log(json.results[0].text); However I am not confident that my observable array is working, when I push the json.results into recent tweets: recent_tweets.push(json.results[0].text) I see an [] empty array.

What is going on? Is logging ko.observable array possible?

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

  console.log("TwitterFeedComponent() loaded")

  var ponent = this;  
  var url = '.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 = '.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);

  });

 }); 
};

I am new to knockoutjs and I have an uber-basic question for you:

I have been able to successfully subscribe to a user changing the on screen twitter handle AND successfully fetch the tweets and display the last recent tweet of a user using console.log(json.results[0].text); However I am not confident that my observable array is working, when I push the json.results into recent tweets: recent_tweets.push(json.results[0].text) I see an [] empty array.

What is going on? Is logging ko.observable array possible?

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

  console.log("TwitterFeedComponent() loaded")

  var ponent = this;  
  var url = 'https://twitter./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./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);

  });

 }); 
};
Share Improve this question asked Dec 29, 2011 at 20:30 JZ.JZ. 22k33 gold badges119 silver badges193 bronze badges 1
  • 1 You need to log the underlying array like console.log(recent_tweets()) – RP Niemeyer Commented Dec 29, 2011 at 20:41
Add a ment  | 

2 Answers 2

Reset to default 4

To access the actual values of an observable whether it's an array or not you need include parenthesis. For example the following will work:

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

The same is true when assigning variables.

Here is an example of a regular scalar value:

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")

To answer this a little differently, you could always use Knockout's subscribe() functionality. Let's assume you have the following view-model:

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

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

For demonstrations sake, let's assume this property is bound to a text field, as follows:

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

Now let's assume that you'd like to log any time this value changes. To do this, simply update your view-model as follows:

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

    self.TestProperty = ko.observable(null);
    self.TestProperty.subscribe(function(newValue){
        console.log("The new value is: " + newValue);
    });
}
发布评论

评论列表(0)

  1. 暂无评论