I have a tree view with a list of cars and a webservice to get the details for each car.
On page load, there are no cars selected so the details footer is loaded with all cars.
$.ajax({
type: 'POST',
url: '@Url.Content("/GTracker/Cars/CarTreeDetails")',
data: { "id": [] }, // not cars filter
traditional: true,
success: function (data) {
$('#footer-details').html(data);
}
});
When the user select cars from the treeview, the details are then updated with only those cars.
This is the changed.jstree
event:
var ajaxDetails;
$('#carTree').on('changed.jstree', function (e, data) {
var i, j, p = [];
// create the id[] array
for (i = 0, j = data.selected.length; i < j; i++) {
node = data.instance.get_node(data.selected[i]);
if (node.data.type != 'root') {
p.push(parseInt(data.instance.get_node(data.selected[i]).id));
}
}
// cancel previous request
if ((ajaxDetails && ajaxDetails.readyState != 4)) {
ajaxDetails.abort()
}
// get only selected cars info.
ajaxDetails = $.ajax({
type: 'POST',
url: '@Url.Content("/GTracker/Cars/CarTreeDetails")',
data: { "id": p }, //cars list
traditional: true,
success: function (data) {
$('#footer-details').html(data);
}
});
});
But I feel I'm repeating myself. How may I merge those two functions together?
- one for the on_load.
- the other for the js_tree change event.