Created
April 14, 2015 13:43
-
-
Save queerviolet/92b5af12f79f19cf083c to your computer and use it in GitHub Desktop.
AJAX models
This file contains hidden or 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
function Tweet(params) { | |
params = params || {}; | |
this.user = params.user; | |
this.content = params.content; | |
} | |
Tweet.recent = function(limit) { | |
var limit = limit || 50; | |
return $.get('/tweets/recent', { | |
dataType: 'json', | |
data: {limit: limit}, | |
}); | |
}; | |
Tweet.search = function(q) { | |
// TODO | |
}; | |
new Tweet(); | |
new Tweet({user_id: 234, content: 'I just pooped'}); | |
function format(tweet) { | |
return '<div class="tweet">' + tweet.content + '<cite>' + tweet.username + '</cite></div>'; | |
} | |
$(document).ready(function() { | |
Tweet.recent(50) | |
.done(function(tweets) { | |
for (var i = 0; i != tweets.length; ++i) { | |
$(document.body).append(format(tweets[i])); | |
} | |
}) | |
.fail(function(error) { alert(error); }); | |
}); | |
/* | |
class Tweet < ActiveRecord::Base | |
belongs_to :user | |
has_many :hashtags | |
def self.recent(limit=50) | |
order(created_at: :descending).limit(limit) | |
end | |
end | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment