是否可以记录 KO 可观察数组?

发布于 2024-12-23 07:04:25 字数 1216 浏览 0 评论 0原文

我是 knockoutjs 的新手,我有一个超级基本的问题要问你:

我已经能够成功订阅用户更改屏幕上的 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);

  });

 }); 
};

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

  });

 }); 
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼藏柔 2024-12-30 07:04:25

要访问可观察量的实际值,无论它是否是数组,您都需要包含括号。例如,以下内容将起作用:

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

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")
染柒℉ 2024-12-30 07:04:25

要以稍微不同的方式回答这个问题,您始终可以使用 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);
    });
}

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);
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文