Skip to content

Instantly share code, notes, and snippets.

@odinserj
Created September 27, 2012 13:20
Show Gist options
  • Save odinserj/3793942 to your computer and use it in GitHub Desktop.
Save odinserj/3793942 to your computer and use it in GitHub Desktop.
jQuery ->
$("#e6").select2
placeholder: "Search for a movie"
minimumInputLength: 1
ajax: # instead of writing the function to execute the request we use Select2's convenient helper
url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json"
dataType: 'jsonp'
data: (term, page) ->
return {
q: term # search term
page_limit: 10
apikey: "ju6z9mjyajq2djue3gbvv26t" # please do not use so this example keeps working
}
results: (data, page) -> # parse the results into the format expected by Select2.
# since we are using custom formatting functions we do not need to alter remote JSON data
return { results: data.movies }
query: (query) ->
data results: []
for i in [1...5]
s = s + query.term for j in [0...i]
data.results.push
id: query.term + u
text: s
query.callback data
formatResult: movieFormatResult, # omitted for brevity, see the source of this page
formatSelection: movieFormatSelection, # omitted for brevity, see the source of this page
dropdownCssClass: "bigdrop" # apply css that makes the dropdown taller
$(function () {
$("#e6").select2({
placeholder: "Search for a movie",
minimumInputLength: 1,
ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json",
dataType: 'jsonp',
data: function (term, page) {
return {
q: term, // search term
page_limit: 10,
apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
};
},
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 remote JSON data
return {results: data.movies};
}
},
query: function (query) {
var data = {results: []}, i, j, s;
for (i = 1; i < 5; i++) {
s = "";
for (j = 0; j < i; j++) {s = s + query.term;}
data.results.push({id: query.term + i, text: s});
}
query.callback(data);
},
formatResult: movieFormatResult, // omitted for brevity, see the source of this page
formatSelection: movieFormatSelection, // omitted for brevity, see the source of this page
dropdownCssClass: "bigdrop" // apply css that makes the dropdown taller
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment