From 9b7f349b38a25e7890e5040dcf508238335440c2 Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Mon, 15 Sep 2014 17:18:16 +0300 Subject: [PATCH] JSON can be an array of objects. Different implementation of #32. --- jquery.chained.remote.js | 16 ++++++-- test/spec/ChainedRemoteSpec.js | 71 ++++++++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 7 deletions(-) diff --git a/jquery.chained.remote.js b/jquery.chained.remote.js index bee5308..bd651af 100644 --- a/jquery.chained.remote.js +++ b/jquery.chained.remote.js @@ -98,9 +98,19 @@ var option_list = []; if ($.isArray(json)) { - /* JSON is already an array (which preserves the ordering of options) */ - /* [["","--"],["series-1","1 series"],["series-3","3 series"]] */ - option_list = json; + if ($.isArray(json[0])) { + /* JSON is already an array of arrays. */ + /* [["","--"],["series-1","1 series"],["series-3","3 series"]] */ + option_list = json; + } else { + /* JSON is an array of objects. */ + /* [{"":"--"},{"series-1":"1 series"},{"series-3":"3 series"}] */ + option_list = $.map(json, function(value) { + return $.map(value, function(value, index) { + return [[index, value]]; + }); + }); + } } else { /* JSON is an JavaScript object. Rebuild it as an array. */ /* {"":"--","series-1":"1 series","series-3":"3 series"} */ diff --git a/test/spec/ChainedRemoteSpec.js b/test/spec/ChainedRemoteSpec.js index 411e9d4..0e978a3 100644 --- a/test/spec/ChainedRemoteSpec.js +++ b/test/spec/ChainedRemoteSpec.js @@ -111,13 +111,21 @@ describe("Remote version of plugin", function() { ' ' + ' ' + ' ' + - ' ' + ' ' + ' ' + ' ' + ' ' + + ' ' + + ' ' + ''); }); @@ -368,9 +376,64 @@ describe("Remote version of plugin", function() { }); }); - it("should be able to bootstrap values", function() { - expect($("#mark2 > option").size()).toBe(3); - expect($("#series2 > option").size()).toBe(6); + it("should be able to bootstrap with array of objects", function() { + $("#series5").remoteChained({ + parents : "#mark5", + url: "/api/series", + loading : "--", + bootstrap : [ + {"--":"--"}, + {"series-2":"2 series"}, + {"series-3":"3 series"}, + {"series-5":"5 series"}, + {"series-6":"6 series"}, + {"series-7":"7 series"}, + {"selected":"series-3"} + ] + }); + + expect($("#series5 > option").size()).toBe(6); + expect($("#series5 > option:selected").val()).toBe("series-3"); + }); + + it("should be able to bootstrap with array of arrays", function() { + $("#series5").remoteChained({ + parents : "#mark5", + url: "/api/series", + loading : "--", + bootstrap : [ + ["--", "--"], + ["series-2", "2 series"], + ["series-3", "3 series"], + ["series-5", "5 series"], + ["series-6", "6 series"], + ["series-7", "7 series"], + ["selected", "series-5"] + ] + }); + + expect($("#series5 > option").size()).toBe(6); + expect($("#series5 > option:selected").val()).toBe("series-5"); + }); + + it("should be able to bootstrap with object", function() { + $("#series5").remoteChained({ + parents : "#mark5", + url: "/api/series", + loading : "--", + bootstrap : { + "--":"--", + "series-2":"2 series", + "series-3":"3 series", + "series-5":"5 series", + "series-6":"6 series", + "series-7":"7 series", + "selected":"series-6" + } + }); + + expect($("#series5 > option").size()).toBe(6); + expect($("#series5 > option:selected").val()).toBe("series-6"); }); it("should honour selected attribute in bootstrapped values", function() {