Skip to content

Instantly share code, notes, and snippets.

@Integral
Last active December 19, 2015 18:29
Show Gist options
  • Save Integral/5998949 to your computer and use it in GitHub Desktop.
Save Integral/5998949 to your computer and use it in GitHub Desktop.
/**
* Select2
*
* Renders Select2 - jQuery based replacement for select boxes
*
* Simply pass a 'config' object on your schema, with any options to pass into Select2.
* See http://ivaynberg.github.com/select2/#documentation
*/
Backbone.Form.editors.Select2Rel = Backbone.Form.editors.Base.extend({
/**
* @param {Object} options.schema.config Options to pass to select2. See http://ivaynberg.github.com/select2/#documentation
*/
initialize: function(options) {
Backbone.Form.editors.Base.prototype.initialize.call(this, options);
var schema = this.schema;
this.config = schema.config || {};
if (options.schema.config.multiple == true) {
var values = options.model.get(options.key),
data = [];
_.each(values, function(obj) {
data.push({id: obj.id, value: obj.value});
});
}
else {
var value = options.model.get(options.key);
data = {id: value.id, value: value.value};
}
},
render: function() {
var self = this;
setTimeout(function() {
self.$el.select2(self.config);
self.setValue(self.value);
}, 0);
return this;
},
getValue: function() {
return this.$el.select2('val');
},
setValue: function(val) {
this.$el.select2('val',val);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment