-
Notifications
You must be signed in to change notification settings - Fork 1
/
questions_show.js
82 lines (66 loc) · 2.03 KB
/
questions_show.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Inquisit.Views.QuestionsShow = Backbone.View.extend({
template: JST['questions/show'],
events: {
"submit form#new-answer": "newAnswer"
},
initialize: function(question) {
this.question = question
this.listenTo(this.question.get('answers'), 'add', this.render);
this.listenTo(this.question.get('answers'), 'remove', this.render);
this.subViews = _([]);
},
render: function() {
var that = this;
this._removeSubViews()
var renderedContent = this.template({
currentUser: Inquisit.currentUser,
question: this.question
});
this.$el.html(renderedContent);
_([
['div#topics', Inquisit.Views.SubViews.Topics, {attribute: 'topics'}],
['div#title', Inquisit.Views.SubViews.Title, {attribute: 'title'}],
['div#details', Inquisit.Views.SubViews.Details, {attribute: 'details'}]
]).each(function(subViewParams) {
that._addSubView.apply(that, subViewParams)
});
var $answerList = this.$el.find('ul#answers-list');
this.question.get('answers').each(function(answer) {
var answerView = new Inquisit.Views.AnswersShow({
answer: answer
});
that.subViews.push(answerView);
$answerList.append(answerView.render().$el);
});
return this;
},
newAnswer: function(event) {
event.preventDefault();
var $form = $(event.target)
this.question.get('answers').create(
$form.serializeJSON().answer, {
wait: true,
url: $form.attr('action'),
success: function(answer) {
// reset url to normal
answer.url = Backbone.Model.prototype.url
}
}
);
},
close: function() {
this._removeSubViews();
},
_removeSubViews: function() {
this.subViews.each(function(subView) {
subView.remove();
});
},
_addSubView: function(cssSelector, View, binding) {
var $el = this.$el.find(cssSelector);
view = new View(_.extend({model: this.question}, binding));
$el.html(view.renderShow().$el);
this.subViews.push(view);
return view;
}
});