Skip to content
Browse files

refactored posts and comments view

  • Loading branch information...
1 parent ee48d37 commit 59894282d5885f220b2f2cc5df0311665e1fd854 @dodo dodo committed Feb 28, 2012
Showing with 50 additions and 45 deletions.
  1. +12 −21 src/views/channel/comments.coffee
  2. +9 −24 src/views/channel/posts.coffee
  3. +29 −0 src/views/channel/postsbase.coffee
View
33 src/views/channel/comments.coffee
@@ -1,16 +1,16 @@
-{ BaseView } = require '../base'
+{ PostsBaseView } = require './postsbase'
{ PostView } = require './post'
{ EventHandler } = require '../../util'
-class exports.CommentsView extends BaseView
+class exports.CommentsView extends PostsBaseView
+ ns: 'comment'
template: require '../../templates/channel/comments'
initialize: ->
super
- @views = {}
# @model.bind 'change', @render
- @model.forEach @add_comment
- @model.bind 'add', @add_comment
+ @model.forEach @add_post
+ @model.bind 'add', @add_post
events:
'keydown .answer textarea': 'hitEnterOnComment'
@@ -52,27 +52,18 @@ class exports.CommentsView extends BaseView
console.error "postError", error
@show_comment_error error
+ createView: (opts = {}) ->
+ opts.type ?= 'comment'
+ new PostView opts
+
+ indexOf: (model) ->
+ @model.indexOf(model)
+
show_comment_error: (error) =>
p = $('<p class="postError"></p>')
@$('.answer .controls').prepend(p)
p.text(error.text or error.condition)
- add_comment: (comment) =>
- view = @views[comment.cid] ?= new PostView
- type:'comment'
- model:comment
- parent:this
- return if view.rendering
- i = @model.indexOf(view.model)
- @ready =>
- @trigger('view:comment:insert', i, (done) ->
- view.ready ->
- view.domready ->
- view.__defineGetter__('_jquery',->view.el) # FIXME wtfuck?
- done()
- )
- view.render()
-
render: (callback) ->
super ->
View
33 src/views/channel/posts.coffee
@@ -1,7 +1,8 @@
-{ BaseView } = require '../base'
+{ PostsBaseView } = require './postsbase'
{ TopicPostView } = require './topicpost'
-class exports.PostsView extends BaseView
+class exports.PostsView extends PostsBaseView
+ ns: 'topic'
template: require '../../templates/channel/posts'
# tutorial: require '../../templates/channel/tutorial.eco'
# empty: require '../../templates/channel/empty.eco'
@@ -10,7 +11,6 @@ class exports.PostsView extends BaseView
# @el will be passed by @parent
# @model is a PostsNode
initialize: ->
- @views = {}
super
@model.posts.forEach @add_post
@model.posts.bind 'add', @add_post
@@ -21,27 +21,12 @@ class exports.PostsView extends BaseView
app.handler.data.refresh_channel channel.get('id')
, 50
- ##
- # TODO add different post type switch here
- # currently only TopicPosts are supported
- add_post: (post) =>
- @$('.tutorial, .empty').remove()
- view = @views[post.cid] ?= new TopicPostView
- model:post
- parent:this
- return if view.rendering
- i = @model.posts.indexOf(view.model)
-# console.error "============================", i
- @ready =>
- @trigger('view:topic:insert', i, (done) ->
-# console.error "insert", i, view.cid
- view.ready ->
- view.domready ->
-# console.error "domeready => ", i, view.el
- view.__defineGetter__('_jquery',->view.el) # FIXME wtfuck?
- done()
- )
- view.render()
+ createView: (opts = {}) ->
+ new TopicPostView opts
+
+ indexOf: (model) ->
+ @model.posts.indexOf(model)
+
# FIXME this code should work again, i guess
# @$('.tutorial, .empty').remove()
View
29 src/views/channel/postsbase.coffee
@@ -0,0 +1,29 @@
+{ BaseView } = require '../base'
+
+class exports.PostsBaseView extends BaseView
+
+ initialize: ->
+ @views = {}
+ super
+
+ ##
+ # TODO add different post type switch here
+ # currently only TopicPosts are supported
+ add_post: (post) =>
+ view = @views[post.cid] ?= @createView
+ model:post
+ parent:this
+ return if view.rendering
+ i = @indexOf(view.model)
+# console.error "============================", i
+ @ready =>
+ @trigger("view:#{@ns}:insert", i, (done) ->
+# console.error "insert", i, view.cid
+ view.ready ->
+ view.domready ->
+# console.error "domeready => ", i, view.el
+ view.__defineGetter__('_jquery',->view.el) # FIXME wtfuck?
+ done()
+ )
+ view.render()
+

0 comments on commit 5989428

Please sign in to comment.
Something went wrong with that request. Please try again.