Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

stuff works maybe

  • Loading branch information...
commit db48814d622a530196742a3201b562b76ba3d033 1 parent a94a329
@agrberg agrberg authored
View
10 app/models/search.rb
@@ -1,9 +1,15 @@
class Search
include Mongoid::Document
field :term
- fattr(:tweets) do
- Twitter::Search.new(term)
+ before_save do |obj|
+ obj.tweets = get_tweets if obj.tweets.empty?
end
+ def get_tweets
+ res = []
+ Twitter::Search.new(term).each { |x| res << x }
+ res
+ end
+ embeds_many :tweets
def to_json(*args)
res = attributes.clone
res['tweets'] = tweets
View
5 app/models/tweet.rb
@@ -0,0 +1,5 @@
+class Tweet
+ include Mongoid::Document
+ field :from_user
+ field :text
+end
View
8 app/views/searches/index.html.haml
@@ -3,6 +3,14 @@
= f.label :term
= f.text_field :term
= f.submit 'Search'
+ #searches
+ #results
#search-template.template
<%= term %>
+
+#tweet-template.template
+ .user
+ <%= from_user %>
+ .body
+ <%= text %>
View
54 public/javascripts/main.js
@@ -21,7 +21,7 @@ $(function(){
window.SearchView = Backbone.View.extend({
- tagName: 'div',
+ el: $('#results'),
template: _.template($('#search-template').text()),
@@ -35,45 +35,69 @@ $(function(){
var me = this
$(this.el).html(this.template(this.model.toJSON()));
- console.debug("tweets size " + this.model.get('tweets').length)
_.each(this.model.get('tweets'),function(tweet) {
- $(me.el).append(tweet['text'])
+ var tv = new TweetView({model: tweet})
+ tv.render()
+ $(me.el).append(tv.el)
})
-
- this.render_callback(this)
return this;
}
});
+ window.TweetView = Backbone.View.extend({
+ tagName: "div",
+
+ template: _.template($('#tweet-template').html().replace("&lt;","<").replace("&gt;",">").replace("&lt;","<").replace("&gt;",">")),
+
+ initialize: function() {
+ _.bindAll(this, 'render');
+ },
+
+ render: function() {
+ console.debug(this.model)
+ $(this.el).html(this.template(this.model));
+ return this;
+ }
+ })
+
window.AppView = Backbone.View.extend({
el: $('#main'),
events : {
- "change #search_term" : "render"
+ "change #search_term" : "render",
+ "click a.search" : "doThing"
},
initialize: function() {
- _.bindAll(this, 'render')
+ _.bindAll(this, 'render','doThing')
},
get_term: function() {
return $('#search_term').val();
},
-
- render: function() {
+
+ doThing: function(event) {
+ this.showTerm($(event.target).text(),false)
+ },
+
+ showTerm: function(term,new_term) {
var me = this
- var model = new Search({term: this.get_term()})
+ var model = new Search({term: term})
Searches.add(model)
model.save()
- //model.fetch()
- var v = new SearchView({model: model})
+ var link = $("<div>")
+ var a = $("<a>").attr("href","#").attr('class','search').text(term)
+ link.append(a)
+ if (new_term) this.$('#searches').append(link)
- v.render_callback = function(v) {
- $(me.el).append(v.el)
- }
+ var v = new SearchView({model: model})
+ },
+
+ render: function() {
+ this.showTerm(this.get_term(),true)
}
})
View
16 public/stylesheets/base.css
@@ -1,3 +1,17 @@
.template {
visibility: hidden;
-}
+}
+
+#main {
+ height: 700px;
+}
+
+#searches {
+ float: left;
+ width: 10%;
+ height: 100%;
+}
+
+#results {
+
+}
View
5 spec/models/tweet_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Tweet do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.