1
我試圖完成以下任務:Knockoutjs超級基本
- 列表項
- 獲取Twitter的手柄
- 詢問用戶有多少鳴叫,顯示
- 取鳴叫
- 僅顯示用戶指定的推文
我有一個超級基本問題。我有硬編碼的HTML,我可以用knockouk獲取推文,我想消除我的硬編碼元素,只使用knockoutjs。我可以使用訂閱,並使用可觀察數組推送推文,從而從用戶Y獲取x個推文。
代碼奇效。下面是我該怎麼做:
TwitterGet = function() {
var recent_tweets = ko.observableArray();
var twitter_image = ko.observable();
var component = this;
var url = 'https://twitter.com/search.json?callback=?';
this.attributes.twitter_user_handle.subscribe(function(value) {
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) {
twitter_image(json.results[0].profile_image_url);
result = json.results;
recent_tweets.push(result);
});
});
};
我的問題是超級簡單。鳴叫住在這裏:
- recent_tweets.slice(-1)[0] [0]的.text(第一鳴叫)
- recent_tweets.slice(-1)[0] [1]的.text(第二推特)
現在我靜靜地把每條推文插入html。如果用戶只有3條推文,並且我已經將5條推文硬編碼到html中,則會中斷。我如何使用knockout插入html與推文?
我想消除的STATIC HTML的示例,並替換爲由Knockout JS插入的動態HTML。
<div class="tweet" id="first-tweet">
<span class="handle"><a href="#" target="_blank" data-bind-component_<%=component.id-%>="inline_edit: attributes.twitter_user_handle"></a>
</span><span data-bind-component_<%=component.id-%>="inline_edit: recent_tweets.slice(-1)[0][1].text"></span><br>
<a href="#">share</a>
<a href="#" target="_blank">retweet</a>
<a href="#">reply</a></div>
<div class="tweet" id="second-tweet">
<span class="handle"><a href="#" target="_blank" data-bind-component_<%=component.id-%>="inline_edit: attributes.twitter_user_handle"></a>
</span><span data-bind-component_<%=component.id-%>="inline_edit: recent_tweets.slice(-1)[0][2].text"></span><br>