summaryrefslogtreecommitdiff
path: root/public/assets/js/lib/views/stream/hootform.js
blob: 07533d5fe2657c3ae1ae106137eb8fa6e3dae8d3 (plain)
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
var HootForm = FormView.extend({
  el: "#hootform",

  events: {},

  action: "/api/thread/1/comment",

  initialize: function (opt) {
    this.__super__.initialize.call(this, opt);
    this.parent = opt.parent;
    this.$comment = this.$("[name=comment]");
  },

  show: function () {
    this.$el.show();
  },

  hide: function () {
    this.$el.hide();
  },

  toggle: function (state) {
    if (state) {
      this.show();
    } else {
      this.hide();
    }
  },

  load: function (comments) {
    if (!comments || (!comments.length && !this.options.required)) {
      this.$el.hide();
      return;
    }
    comments.forEach(this.appendComment.bind(this));
  },

  validate: function () {
    var errors = [];
    var comment = $("[name=comment]").val();
    if (!comment || !comment.length) {
      errors.push("Please enter a comment.");
    }
    return errors.length ? errors : null;
  },

  success: function (data) {
    this.parent.onComment(data.comment);
    this.$("[name=comment]").val("");
  },
});