是否可以记录 KO 可观察数组?
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要访问可观察量的实际值,无论它是否是数组,您都需要包含括号。例如,以下内容将起作用:
分配变量时也是如此。
以下是常规标量值的示例:
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:
The same is true when assigning variables.
Here is an example of a regular scalar value:
要以稍微不同的方式回答这个问题,您始终可以使用 Knockout 的 subscribe() 功能。假设您有以下视图模型:
为了演示起见,我们假设此属性绑定到文本字段,如下所示:
现在假设您希望在该值更改时记录任何时间。为此,只需按如下方式更新您的视图模型:
To answer this a little differently, you could always use Knockout's subscribe() functionality. Let's assume you have the following view-model:
For demonstrations sake, let's assume this property is bound to a text field, as follows:
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: