-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (96 loc) · 2.67 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
Backbone-Forms Bootstrap TypeAhead editor 2.0.0
Copyright (c) 2016 Tomasz Jakub Rup
https://github.com/tomi77/backbone-forms-typeahead
Released under the MIT license
*/
(function(root, factory) {
switch (false) {
case !(typeof define === 'function' && define.amd):
define(['underscore', 'backbone', 'backbone-forms', 'typeahead.js'], factory);
break;
case typeof exports !== 'object':
require('backbone-forms');
require('typeahead.js');
factory(require('underscore'), require('backbone'));
break;
default:
factory(root._, root.Backbone);
}
})(this, function(_, Backbone) {
var Base, Form, Text;
Form = Backbone.Form;
Base = Form.editors.Base;
Text = Form.editors.Text;
/*
Additional editors that depend on Twitter TypeAhead
*/
Form.editors.TypeAhead = Text.extend({
initialize: function(options) {
var ref;
Base.prototype.initialize.call(this, options);
_.bindAll(this, 'renderOptions');
this.$el.attr({
type: 'text',
'data-provide': 'typeahead',
autocomplete: 'off'
});
if (((ref = this.schema) != null ? ref.options : void 0) == null) {
throw new Error("Missing required 'schema.options'");
}
},
/*
Adds the editor to the DOM
*/
render: function() {
this.setValue(this.value);
this.setOptions(this.schema.options);
return this;
},
/*
Sets the options that populate the data-source attribute
@param {Mixed} options
*/
setOptions: function(options) {
switch (false) {
case !(options instanceof Backbone.Collection):
if (options.length > 0) {
this.renderOptions(options);
} else {
options.fetch({
success: this.renderOptions
});
}
break;
case !_.isFunction(options):
options(this.renderOptions, this);
break;
default:
this.renderOptions(options);
}
},
/*
Adds the data-source attribute to the input element
@param {Mixed} Options as a simple array e.g. ['option1', 'option2']
or as a Backbone collection
*/
renderOptions: function(options) {
var source;
source = (function() {
switch (false) {
case !_.isArray(options):
return options;
case !(options instanceof Backbone.Collection):
return options.map(function(row) {
return row.toString();
});
}
})();
this.$el.data({
items: source.length,
source: source
});
this.setValue(this.value);
}
});
});