Created
October 7, 2011 13:51
-
-
Save csainty/1270329 to your computer and use it in GitHub Desktop.
Knockout Blog Post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul data-bind="template: { name : 'tweetTemplate', foreach: tweets }"></ul> | |
<input type="text" placeholder="search..." data-bind="value: searchTerm" /> | |
<button data-bind="click: search">Search</button> | |
<script type="text/x-jquery-tmpl" id="tweetTemplate"> | |
<li class="tweet"> | |
<img src="${profile_image_url}" alt="${from_user}" /> | |
<p class="content"> | |
${from_user}: ${text}<br /> | |
</p> | |
<p class="datetime">${created_at}</p> | |
<div class="ui-helper-clearfix"></div> | |
</li> | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var viewModel = { | |
searchTerm: ko.observable(""), | |
tweets: ko.observableArray([]), | |
search: searchTwitter | |
} | |
ko.applyBindings(viewModel); | |
function searchTwitter() { | |
$.ajax({ | |
url: 'http://search.twitter.com/search.json', | |
dataType: 'jsonp', | |
data: { | |
q: viewModel.searchTerm(), | |
result_type: 'recent', | |
rpp: 20, | |
lang: 'en' | |
}, | |
success: handleTwitterResponse | |
}); | |
} | |
function handleTwitterResponse(result) { | |
for (var index = result.results.length - 1; index >= 0; index--) { | |
viewModel.tweets.unshift(result.results[index]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment