Created
May 30, 2013 07:53
-
-
Save snize/5676348 to your computer and use it in GitHub Desktop.
Meteor YouTube Search
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
<head> | |
<meta charset="UTF-8"> | |
<title>Meteor YouTube Search</title> | |
</head> | |
<body> | |
<h1>YouTube Search</h1> | |
{{> youtubeSearch}} | |
</body> | |
<template name="youtubeSearch"> | |
<div> | |
<form name="myForm"> | |
<input type="text"> | |
<input type="button" value="search" disabled> | |
</form> | |
<h2>Results</h2> | |
{{#if foo}} | |
<ul style="list-style:none;padding:0"> | |
{{#each results}} | |
<li> | |
<img src="{{this.media$group.media$thumbnail.[0].url}}"> | |
{{this.title.$t}} | |
</li> | |
{{/each}} | |
</ul> | |
{{/if}} | |
{{#unless foo}}<p>↑ 上から検索してください。</p>{{/unless}} | |
</div> | |
</template> |
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
if (Meteor.isClient) { | |
Template.youtubeSearch.results = function () { | |
return Session.get("responsYoutube") || []; | |
} | |
Template.youtubeSearch.helpers({ | |
foo: function () { | |
return Session.get("responsYoutube") !== undefined; | |
} | |
}); | |
Template.youtubeSearch.events({ | |
'click input[type=button]': function (event, template) { | |
var url = 'https://gdata.youtube.com/feeds/api/videos?' | |
+ [ | |
'q=' + encodeURIComponent(template.find("input").value), | |
'alt=json', | |
'max-results=10', | |
'v=2', | |
'callback=?' | |
].join('&'); | |
$.getJSON(url, function (data, status) { | |
Session.set("responsYoutube", data.feed.entry); | |
} | |
); | |
}, | |
'input input[type=text]': function (event, template) { | |
template.find("input[type=button]").disabled = false; | |
} | |
}); | |
} | |
if (Meteor.isServer) { | |
Meteor.startup(function () { | |
// code to run on server at startup | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment