1

私はノックアウトjsが初めてで、あなたに非常に基本的な質問があります:

画面上の twitter ハンドルを変更するユーザーを正常にサブスクライブし、ツイートを正常にフェッチして、ユーザーの最後の最近のツイートを表示することができまし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);

  });

 }); 
};
4

2 に答える 2

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")
于 2011-12-29T20:39:52.430 に答える
1

これに少し違う答えを出すには、Knockout の subscribe() 機能をいつでも使用できます。次のビューモデルがあるとします。

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);
    });
}
于 2015-03-25T18:19:55.110 に答える