1
백본을 조금 더 사용하려고 시도 중입니다. 과거에 백본보기 만 사용한 사람에게서 모델 및 컬렉션을 사용하고 있습니다.백본 모델 방법이 증가하지 않음
지금 바로 의견을 게시 할 때 댓글 수를 늘리려고합니다.
모델 :
Comment = Backbone.Model.extend({
defaults: {
text: null,
count: 0
},
updateCount : function() {
console.log(this.set('count', this.get('count') + 1));
console.log(this.get('count'));
}
});
컬렉션 :
CommentsCollection = Backbone.Collection.extend({
model: Comment,
initialize: function (models, options) {
this.on("add", options.view.appendComment);
this.on('add', options.view.resetComment);
}
});
보기 :
CommentsView = Backbone.View.extend({
el: $("body"),
initialize: function() {
_.bindAll(this,
'addComment',
'appendComment',
'resetComment'
);
this.comments = new CommentsCollection(null, {
model: Comment,
view: this
});
},
events: {
"click #post-comment": "addComment"
},
addComment: function (evt) {
var $target = $(evt.currentTarget);
var $container = $target.closest('#comment-wrapper');
var text = $container.find('textarea').val();
var comment = new Comment({
text: text
});
//Add a new comment model to our comment collection
this.comments.add(comment);
return this;
},
appendComment: function (model) {
$('#comments').prepend('<div> ' + model.get('text') + '</div>');
model.updateCount();
return this;
},
resetComment: function() {
$('textarea').val('');
}
});
왜 항상 코멘트를 추가 한 다음 확인하기 위해 콘솔을 표시 게시를 클릭합니다 (1을 반환)?
데모 : http://jsfiddle.net/ZkBWZ/