Created
April 12, 2012 14:44
-
-
Save jokull/2367825 to your computer and use it in GitHub Desktop.
CoffeScript "ify" view and model for Tweets
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
class window.Tweet extends Backbone.Model | |
initialize: (options) -> | |
if @has "text" | |
tweet = @ify.hash (@get "text") | |
tweet = @ify.at tweet | |
tweet = @ify.link tweet | |
@set ified: tweet | |
ify: | |
link: (t) -> | |
t.replace /(^|\s+)(https*\:\/\/\S+[^\.\s+])/g, (m, m1, link) -> | |
m1 + "<a href=" + link + ">" + (if (link.length > 25) then link.substr(0, 24) + "..." else link) + "</a>" | |
at: (t) -> | |
t.replace /(^|\s+)\@([a-zA-Z0-9_]{1,15})/g, (m, m1, m2) -> | |
m1 + "@<a href=\"http://twitter.com/" + m2 + "\">" + m2 + "</a>" | |
hash: (t) -> | |
t.replace /(^|\s+)\#([a-zA-Z0-9_]+)/g, (m, m1, m2) -> | |
m1 + "#<a href=\"http://search.twitter.com/search?q=%23" + m2 + "\">" + m2 + "</a>" | |
class window.TweetView extends Backbone.View | |
el: "#tweet" | |
initialize: (options) -> | |
@model.bind "change", @render | |
@render() | |
render: => | |
tpl = _.template ($ "#tpl-tweet").html() | |
($ @el).html (tpl @model.toJSON()) | |
return @ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment