From d14631325686d7bfe5b1c63485c407fc85b68491 Mon Sep 17 00:00:00 2001
From: Kevin Brown
Date: Sat, 14 Mar 2015 19:58:50 -0400
Subject: [PATCH] More about `initSelection`
In the past, `initSelection` was used for loading in data objects
for the pre-selected options. Now that Select2 is using a `
+
+ If you only need to load in the initial options once, and otherwise will
+ be letting Select2 handle the state of the selections, you don't need to
+ use a custom data adapter. You can just create the
+ <option> tags on your own, and Select2 will pick up
+ the changes.
+
+
+
+var $element = $('select').select2(); // the select element you are working with
+
+var $request = $.ajax({
+ url: '/my/remote/source' // wherever your data is actually coming from
+});
+
+$request.then(function (data) {
+ // This assumes that the data comes back as an array of data objects
+ // The idea is that you are using the same callback as the old `initSelection`
+
+ for (var d = 0; d < data.length; d++) {
+ var item = data[d];
+
+ // Create the DOM option that is pre-selected by default
+ var option = new Option(data.text, data.id, true, true);
+
+ // Append it to the select
+ $element.append(option);
+ }
+
+ // Update the selected options that are displayed
+ $element.trigger('change');
+});
+
+
Custom data adapters instead of query
@@ -643,7 +677,7 @@ var data = $.map([
-$("select").val("1"); // instead of $("select").select2("val", "1");
+$("select").val("1").trigger("change"); // instead of $("select").select2("val", "1");
.select2("enable")
diff --git a/docs/index.html b/docs/index.html
index 4bac1dc7..1fb5848d 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -170,17 +170,6 @@ slug: home
jquery.mousewheel
-
-
- AMD (select2.amd.js / select2.amd.full.js)
-
-
- This is the build that anyone who is using Select2 with an existing
- AMD build system should use. It is also recommended that you read
- the AMD compatibility documentation
- to avoid any unexpected issues.
-