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
   // Posts twice
   $("select.label").chosen({
      create_option: function(term) {
        var chosen = this,
          questionId = $(chosen.form_field).attr("data-question");
        if (questionId) {
          var label = {
            value: term.trim(),
            question: "/api/profileQuestions/"+questionId
          }
          console.log("Posted label: ", label);
          $.ajax({
            type: 'POST',
            url: '/api/label/',
            data: label,
            success: function(data){
              console.log("Post success: ", data);
              chosen.append_option({
                value: 'value-'+data.term,
                text: data.term
              });
            },
            error: function(data) {
              console.error("Posting error",data.status,": ", data);
            }
          });
        }
        else console.error("Could not find question Id");
      },
      create_option_text: "Create label"
    });

  // Creates one new option
  $("select.label").chosen({
      create_option: function(term) {
        var chosen = this,
          questionId = $(chosen.form_field).attr("data-question");
        if (questionId) {
          var label = {
            value: term.trim(),
            question: "/api/profileQuestions/"+questionId
          }
          console.log("Posted label: ", label);
          chosen.append_option({
            value: 'value-'+label.value,
            text: label.value
          });
        }
        else console.error("Could not find question Id");
      },
      create_option_text: "Create label"
    });