beforeSend ?

393 views
Skip to first unread message

hhu...@gmail.com

unread,
Apr 8, 2013, 4:42:07 PM4/8/13
to sel...@googlegroups.com
Select2 has been great to work with. I have one case where I would like to have the select2 not make an AJAX call based on other values on the page. Looking at the jquery $.ajax docs I found something called beforeSend, so I tried adding this:

beforeSend: function (jqxhr, settings) {
var clientData = $("#client").select2("data");
return clientData && $.isNumeric(clientData.id);
}

It does not appear that select2 looks for a beforeSend, is there a similar facility in select2?

For completeness here is my entire setup:

$("#division").select2({
placeholder: "", // "Division Name",
minimumInputLength: 0,
allowClear: true,
ajax: {
url: _specs.divisionNameAutoCompleteUrl,
quiteMillis: 100,
data: function (term, page) {
var clientData = $("#client").select2("data");

return {
text: term,
page_limit: 10,
page: page,
clientId: clientData.id,
includeInactiveDivisions: $("#includeInactiveDivisions").is(':checked')
}
},
results: function (data, page) {
return { results: data.data, more: data.more };
},
beforeSend: function (jqxhr, settings) {
var clientData = $("#client").select2("data");
return clientData && $.isNumeric(clientData.id);
}
}
});

Thanks for any help.
Tony Nelson

Igor Vaynberg

unread,
Apr 8, 2013, 5:22:53 PM4/8/13
to sel...@googlegroups.com
you can override the transport option (see docs) and in there set
beforeSend on $.ajax instance.

-igor
> --
> You received this message because you are subscribed to the Google Groups "select2" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to select2+u...@googlegroups.com.
> To post to this group, send email to sel...@googlegroups.com.
> Visit this group at http://groups.google.com/group/select2?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

hhu...@gmail.com

unread,
Apr 9, 2013, 9:12:31 AM4/9/13
to sel...@googlegroups.com
On Monday, April 8, 2013 5:22:53 PM UTC-4, Igor Vaynberg wrote:
> you can override the transport option (see docs) and in there set
>
> beforeSend on $.ajax instance.
>
>
>
> -igor
>
>
>
>
Thank you very much Igor. I believe I have it working. Is returning null correct when I want to abort a call? This is my current solution which seems to be working:

$("#division").select2({
placeholder: "", // "Division Name",
minimumInputLength: 0,
allowClear: true,
ajax: {
url: _specs.divisionNameAutoCompleteUrl,
quiteMillis: 100,
data: function (term, page) {
var clientData = $("#client").select2("data");

if (clientData && $.isNumeric(clientData.id)) {

return {
text: term,
page_limit: 10,
page: page,
clientId: clientData.id,
includeInactiveDivisions: $("#includeInactiveDivisions").is(':checked')
}
}

return null;
},
transport: function (params) {


var clientData = $("#client").select2("data");

if (clientData && $.isNumeric(clientData.id)) {
return $.ajax(params);
}

return null;


},
results: function (data, page) {
return { results: data.data, more: data.more };
},
beforeSend: function (jqxhr, settings) {
var clientData = $("#client").select2("data");
return clientData && $.isNumeric(clientData.id);

},
}
});

Reply all
Reply to author
Forward
0 new messages