CommentStream inherits from Base view; using subviews in Post view; be a little smarter about bindAll in Stream view
This commit is contained in:
parent
fe4530f86b
commit
79208e9073
3 changed files with 28 additions and 45 deletions
|
|
@ -1,4 +1,7 @@
|
|||
App.Views.CommentStream = Backbone.View.extend({
|
||||
App.Views.CommentStream = App.Views.Base.extend({
|
||||
|
||||
template_name: "#comment-stream-template",
|
||||
|
||||
events: {
|
||||
"submit form": "createComment",
|
||||
"focus .comment_box": "commentTextareaFocused",
|
||||
|
|
@ -6,28 +9,18 @@ App.Views.CommentStream = Backbone.View.extend({
|
|||
},
|
||||
|
||||
initialize: function(options) {
|
||||
this.model = options.model;
|
||||
this.template = _.template($("#comment-stream-template").html());
|
||||
|
||||
_.bindAll(this, "appendComment");
|
||||
this.model.comments.bind('add', this.appendComment, this);
|
||||
},
|
||||
|
||||
render: function() {
|
||||
$(this.el).html(this.template($.extend(
|
||||
this.model.toJSON(),
|
||||
App.user()
|
||||
)));
|
||||
|
||||
postRenderTemplate : function() {
|
||||
this.$("label").inFieldLabels();
|
||||
|
||||
this.model.comments.each(this.appendComment);
|
||||
this.model.comments.each(this.appendComment, this);
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
createComment: function(evt) {
|
||||
evt.preventDefault();
|
||||
if(evt){ evt.preventDefault(); }
|
||||
|
||||
this.model.comments.create({
|
||||
"text" : this.$(".comment_box").val(),
|
||||
|
|
|
|||
|
|
@ -10,16 +10,23 @@ App.Views.Post = App.Views.StreamObject.extend({
|
|||
"click .block_user": "blockUser"
|
||||
},
|
||||
|
||||
render: function() {
|
||||
// subview rendering
|
||||
this.renderTemplate()
|
||||
.renderPostContent()
|
||||
.renderFeedback()
|
||||
.renderComments();
|
||||
//
|
||||
subviews : {
|
||||
".feedback" : "feedbackView",
|
||||
".comments" : "commentStreamView"
|
||||
},
|
||||
|
||||
this.initializeTooltips();
|
||||
this.$(".details time").timeago();
|
||||
initialize : function() {
|
||||
this.feedbackView = new App.Views.Feedback({model : this.model});
|
||||
this.commentStreamView = new App.Views.CommentStream({ model : this.model});
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
postRenderTemplate : function() {
|
||||
this.renderPostContent()
|
||||
.initializeTooltips()
|
||||
.$(".details time")
|
||||
.timeago();
|
||||
|
||||
return this;
|
||||
},
|
||||
|
|
@ -34,22 +41,6 @@ App.Views.Post = App.Views.StreamObject.extend({
|
|||
return this;
|
||||
},
|
||||
|
||||
renderComments: function(){
|
||||
this.$(".comments").html(new App.Views.CommentStream({
|
||||
model: this.model
|
||||
}).render().el);
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
renderFeedback: function(){
|
||||
this.$(".feedback").html(new App.Views.Feedback({
|
||||
model: this.model
|
||||
}).render().el);
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
removeNsfwShield: function(evt){
|
||||
if(evt){ evt.preventDefault(); }
|
||||
|
||||
|
|
|
|||
|
|
@ -4,20 +4,20 @@ App.Views.Stream = Backbone.View.extend({
|
|||
},
|
||||
|
||||
initialize: function() {
|
||||
_.bindAll(this, "appendPost", "collectionFetched", "loadMore");
|
||||
_.bindAll(this, "collectionFetched");
|
||||
|
||||
this.collection = this.collection || new App.Collections.Stream;
|
||||
this.collection.bind("add", this.appendPost);
|
||||
this.collection.bind("add", this.appendPost, this);
|
||||
},
|
||||
|
||||
render : function(){
|
||||
_.each(this.collection.models, this.appendPost)
|
||||
_.each(this.collection.models, this.appendPost, this)
|
||||
return this;
|
||||
},
|
||||
|
||||
appendPost: function(post) {
|
||||
var postView = new App.Views.Post({ model: post }).render();
|
||||
$(this.el).append(postView.el);
|
||||
var postView = new App.Views.Post({ model: post });
|
||||
$(this.el).append(postView.render().el);
|
||||
},
|
||||
|
||||
collectionFetched: function() {
|
||||
|
|
@ -32,7 +32,6 @@ App.Views.Stream = Backbone.View.extend({
|
|||
if(evt) { evt.preventDefault(); }
|
||||
|
||||
this.addLoader();
|
||||
|
||||
this.collection.fetch({
|
||||
add: true,
|
||||
success: this.collectionFetched
|
||||
|
|
|
|||
Loading…
Reference in a new issue