<html>
<body>
<div id="app">
...
</div>
</body>
</html>
App = Em.Application.create({
rootElement: "#app",
selectedVideo: null
});
App.Video = Em.Object.extend({
title: null,
yid: null
});
App.searchResultsController =
Em.ArrayController.create({
isSearching: false,
search: function(query) {
this.set('isSearching', true);
// search logic
this.set('isSearching', false);
}
});
<script type="text/x-handlebars">
{{view App.SearchBox
placeholder="What you want to watch today?"}}
</script>
App.SearchBox = Em.TextField.extend({
insertNewline: function() {
var query = this.get('value');
App.searchResultsController.search(query);
}
});
search: function(query) {
var self = this;
this.set('isSearching', true);
this.set('content', []);
var c = $.getJSON(
"http://gdata.youtube.com/feeds/api/videos",
{ alt: 'json', 'max-results': 7, v: 2, q: query });
c.success(function(data) {
self.set('content', dataToVideos(data.feed.entry);
});
c.complete(function() {
self.set('isSearching', false);
});
}
dataToVideos: function(entries) {
var results = [];
for (var i = 0; i < entries.length; i++) {
var e = entries[i];
results.push(App.Video.create({
yid: e.id.$t.split(':')[3],
title: e.title.$t
}));
}
return results;
}
<table>
{{#each App.searchResultsController}}
<tr>
<td class="title">{{title}}</td>
</tr>
{{/each}}
</table>
App.ResultRow = Em.View.extend({
video: null,
click: function(evt) {
App.set('selectedVideo', this.get('video'));
}
});
<table>
{{#each App.searchResultsController}}
{{#view App.ResultRow
videoBinding="this" tagName="tr"}}
<td class="title">{{video.title}}</td>
{{/view}}
{{/each}}
</table>
<script type="text/x-handlebars">
{{#view App.Player}}
<h3>{{videoTitle}}</h3>
<iframe {{bindAttr src="videoUrl"}}></iframe>
{{/view}}
</script>
App.Player = Em.View.extend({
videoBinding: "App.selectedVideo",
videoTitle: function() {
var video = this.get('video');
return video ? video.get('title') : "Play video";
}.property('video'),
videoUrl: function() {
return "http://www.youtube.com/embed/" +
this.getPath('.video.yid');
}.property("video")
});
{{#if App.searchResultsController.isSearching}}
Searching. Please wait…
{{/if}}
App.searchResultsController =
Em.ArrayController.create({
isSearching: false,
search: function(query) {
var self = this;
this.set('isSearching', true);
var c = $.getJSON(...);
c.complete(function() {
self.set('isSearching', false);
});
}
});
Aleksey Gureiev
Use a spacebar or arrow keys to navigate