I have some tabs that are loaded with ajax. I get the content via HTML and insert it into the page. The tricky part is when I load the new content with ajax I will need to listen for other events for the new content and perform other tasks on the new content. Here is my object:
var Course = {
config: {
page: 'description'
}
init: function() {
$('a.show-details').on('click', this.loadCourse);
},
loadCourse: function() {
var courseId = $(this).parent().data('course_id'),
item_content = $(this).parents('div.item').children(),
path = 'main/ajaxjson/load_course_details';
if ($(this).parents('h1.more').hasClass('current')) {
$(this).parents('h1.more').removeClass('current');
} else {
$(this).parents('h1.more').addClass('current');
}
$(item_content[2]).slideToggle();
Course.doAjax(courseId, path);
},
doAjax: function(courseId, path) {
$.ajax({
type: 'POST',
url: ROOT_PATH + path,
data: {page : Course.config.page, course_id: courseId},
success: function(result){
$('#ajax-content-' + courseId).hide();
$('#ajax-content-' + courseId).empty().append(result);
$('#ajax-content-' + courseId).show();
// bind events here? call other methods like Couse.methodName()?
}
});
}
}`
I am not providing markup so let me explain the logic. I init the course object and I have a link when I click it i fire an ajax request to get the course and slideToggle the course content. Now I return the html content loaded. In the html I will have buttons and other items. I make a new method bindEvents and bind the events there? Do I need to rebind for every ajax call?