1 define(function(require) {
5 var _ = require('libs/underscore'),
6 $ = require('libs/jquery'),
7 Backbone = require('libs/backbone'),
8 template = require('libs/text!./template.html'),
9 itemTemplate = require('libs/text!./itemTemplate.html');
12 var OpenSelect = Backbone.View.extend({
13 className: 'openSelect',
15 'click a': 'onSelection',
17 initialize: function() {
18 this.$el.css('position', 'relative');
19 this.$el.append(_.template(template)({value: this.options.value || ''}));
20 this.$('.toggle').dropdown();
21 this.menu = this.$('.dropdown-menu');
22 this.toggleHandler(false);
23 if(this.options.inputTemplate) {
24 this.input = $(this.options.inputTemplate);
25 this.$('.input-wrapper').append(this.input);
28 toggleHandler: function(toggle) {
29 this.$('.toggle').css('visibility', toggle ? 'inherit' : 'hidden');
31 addItem: function(value) {
32 this.menu.append(_.template(itemTemplate)({value: value}));
33 this.toggleHandler(true);
35 clearItems: function() {
37 this.toggleHandler(false);
39 setInput: function(value) {
40 if(this.options.setInput) {
41 this.options.setInput(this.input, value);
44 onSelection: function(e) {
45 var val = $(e.target).text();
47 this.trigger('itemSelected', this.input.val());