From 502151b9dce7b0dc5a0674d709088c4937aeb258 Mon Sep 17 00:00:00 2001 From: vitalets Date: Sun, 23 Jun 2013 11:31:46 +0400 Subject: [PATCH] convert source from x-editable format to select2 format --- src/inputs/select2/select2.js | 22 +++++++++++++++++++--- test/unit/select2.js | 4 ++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/inputs/select2/select2.js b/src/inputs/select2/select2.js index 271d212..f4dbfc2 100644 --- a/src/inputs/select2/select2.js +++ b/src/inputs/select2/select2.js @@ -63,9 +63,9 @@ $(function(){ } options.select2.ajax.url = source; } else { - //todo: possible convert x-editable source to select2 source format - options.select2.data = source; - this.sourceData = source; + //check format and convert x-editable format to select2 format (if needed) + this.sourceData = this.convertSource(source); + options.select2.data = this.sourceData; } } @@ -186,6 +186,22 @@ $(function(){ $(this).closest('form').submit(); } }); + }, + + /* + Converts source from x-editable format: {value: 1, text: "1"} to + select2 format: {id: 1, text: "1"} + */ + convertSource: function(source) { + if($.isArray(source) && source.length && source[0].value !== undefined) { + for(var i = 0; itest2').appendTo('#qunit-fixture').editable({ - source: [{id: 1, text: 'text1'}, {id: 2, text: 'text2'}, {id: 3, text: 'text3'}] + source: [{value: 1, text: 'text1'}, {value: 2, text: 'text2'}, {value: 3, text: 'text3'}] }); //autotext