Skip to content

Commit

Permalink
JSON can be an array of objects. Different implementation of #32.
Browse files Browse the repository at this point in the history
  • Loading branch information
tuupola committed Sep 15, 2014
1 parent 7f8edd5 commit 9b7f349
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
16 changes: 13 additions & 3 deletions jquery.chained.remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"} */
Expand Down
71 changes: 67 additions & 4 deletions test/spec/ChainedRemoteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,21 @@ describe("Remote version of plugin", function() {
' <option value="bmw" selected>BMW</option>' +
' <option value="audi">Audi</option>' +
' </select>' +
' <select class="series" id="series3" name="series">' +
' <select id="series3" name="series">' +
' <option value="">--</option>' +
' </select>' +
' <input type="text" class="mark" id="mark4" name="mark">' +
' <select class="series" id="series4" name="series">' +
' <option value="">--</option>' +
' </select>' +
' <select class="mark" id="mark5" name="mark">' +
' <option value="">--</option>' +
' <option value="bmw" selected>BMW</option>' +
' <option value="audi">Audi</option>' +
' </select>' +
' <select class="series" id="series5" name="series">' +
' <option value="">--</option>' +
' </select>' +
'</form>');
});

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 9b7f349

Please sign in to comment.