Skip to content

Instantly share code, notes, and snippets.

@frisk
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frisk/b3b9c60b6654759b1766 to your computer and use it in GitHub Desktop.
Save frisk/b3b9c60b6654759b1766 to your computer and use it in GitHub Desktop.
$(function() {
$("#app").select2({
ajax: {
url: "/dashboard/api/1/apps",
dataType: 'json',
delay: 250,
// the first parameter passed into this
// function is a direct reference to the "term" from the select2
// search box so you don't need to add params.term.
data: function (params) {
return {
name: params,
};
},
/**
* results is the actual name of the select2
* function that gets called internally when
* data is returned from the server so it must
* be named results.
*/
results: function (data, page) {
// parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to
// alter the remote JSON data
/**
* you can't call a function after a return statement.
* Passing a non-string value to alert will only return
* [object Object] so in order to see what will be inside
* data use console.log(data).
*/
console.log(data);
return {
results: data.apps
};
},
cache: true
},
/**
* outside of the ajax options
* there is a formatSelection and a formatResult
* which are responsible for taking what was specified
* in the results function and actually displaying it.
*/
/**
* formatResult will be called on every item
* in the array, so each app in apps will run
* through the formatResult function
*/
formatResult: function (app) {
return app.name;
},
/**
* formatSelection is called once the user
* makes a selection and it determines what
* format the data will be displayed in once a
* selection is made.
*/
formatSelection: function (app) {
return app.name
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 1,
// templateResult: formatRepo, // omitted for brevity, see the source of this page
// templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment