Skip to content

Instantly share code, notes, and snippets.

@jokull
Created September 3, 2011 20:00
Show Gist options
  • Save jokull/1191695 to your computer and use it in GitHub Desktop.
Save jokull/1191695 to your computer and use it in GitHub Desktop.
CoffeeScript Backbone Tumblr with JSONP
<html>
<head>
<script type="text/template" id="tpl-tumblr-post">
<% if(title){ %>
<h2 class="title">
<a href="<%= post_url %>"><%= title %></a>
</h2>
<% } %>
<%= body %>
</script>
</head>
<body>
<div id="tumblr">
</div>
</body>
</html>
window.{Global}.collections = {}
window.{Global}.models = {}
window.{Global}.views = {}
class window.TumblrPost extends Backbone.Model
class window.Tumblr extends Backbone.Collection
model: TumblrPost
class window.TumblrView extends Backbone.View
el: "#tumblr"
initialize: (options) ->
@collection.bind "add", @add
add: (model) =>
model.view = new TumblrPostView model: model
($ @el).append model.view.render().el
class window.TumblrPostView extends Backbone.View
initialize: (options) ->
@model.bind "change", @render
@render()
render: =>
tpl = _.template ($ "#tpl-tumblr-post").html()
($ @el).html (tpl @model.toJSON())
return @
$ ->
$.ajax
url: 'http://api.tumblr.com/v2/blog/{your-blog}.tumblr.com/posts/json?api_key=' + {tumblr_api_key}
dataType: "jsonp"
jsonp: "jsonp"
success: (data, status) =>
{Global}.collections.tumblr = new Tumblr
{Global}.views.tumblr = new TumblrView collection: {Global}.collections.tumblr
{Global}.collections.tumblr.add data.response.posts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment