bobamo /plugins/static/public/js/libs/editors/multi-editor.js

Language Javascript Lines 64
MD5 Hash 7d9ba2c21833cb2dd049e6836964c315
Repository git://github.com/jspears/bobamo.git View Raw File
| Open JSFiddle
 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
define(['Backbone.Form', 'jquery', 'underscore'], function (Form, $, _) {
    var editors = Form.editors;
    var MultiEditor = editors.MultiEditor = editors.Select.extend({
        initialize:function (options) {
            editors.Base.prototype.initialize.call(this, options);

            if (!this.schema || !(this.schema.options || this.schema.url)) throw "Missing required 'schema.options'";
            if (this.schema && ( this.schema.multiple  !== false || this.schema.dataType == 'Array' || this.schema.type == 'Array' )){
                this.$el.attr('multiple', 'multiple');
                this._multiple = true;
            }
        },
        setValue:function (value) {
            if (value)
                $('option', this.el).each(function (k, v) {
                    var $v = $(v);
                    if (value && value.indexOf($v.val()) > -1)
                        $v.attr('selected', 'selected');
                });
            this.value = value;
        },
        selectNone:'<option data-none="null" value="">None</option>',
        renderOptions:function (options) {
            var $select = this.$el,
                html = '';
            var soptions = this.schema.options
            if (!(soptions && soptions.required )) {
              html += soptions && soptions.selectNone || this.selectNone;
            }
            //Accept string of HTML
            if (_.isString(options)) {
                html += options;
            }

            //Or array
            else if (_.isArray(options)) {
                html += this._arrayToHtml(options);
            }

            //Or Backbone collection
            else if (options instanceof Backbone.Collection) {
                html += this._collectionToHtml(options)
            }

            //Insert options
            $select.html(html);

            //Select correct option
            this.setValue(this.value);
        },
        getValue:function(){
            var $children =this.$el.children();
            for(var i=0,l=$children.length; i<l; i++){
                if($children[i].selected && $($children[i]).attr('data-none') == 'null'){
                    return null;
                }
            }
            return this.$el.val();

        }
    });
    return MultiEditor;

});
Back to Top