Php Flow » Jquery

How To Create Generalized Function For Jquery Template

Hi, In previous tutorial I was told you how to use jquery template . In this tutorial we will learn how to create a generalized function to handle all template request in jquery.Normally we are creating each function to handle for each request, but it’s not good practice for developer, Always our first aim to write an optimize code for application.
In this tutorial I will tell you how to write an optimize single jQuery function which will handle all your Jquery template request. We just pass parameters array in this function and get result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function getDetails(params) {
    teamId = jQuery('#active-team-id').val();
    jQuery.ajax({
        url: siteurl + params['targetUrl'],
        cache: false,
        dataType: "html",
        data: {
            action: params['action'],
            teamId: teamId,
            dataID: params['dataID'],
        },
        type: 'Post',
        success: function (response) {
            var $obj = "";
            $obj = jQuery.parseJSON(response);
            jQuery(params['divID']).empty();
            //alert(jQuery(params['divID']).html());
            jQuery(params['templateId']).tmpl($obj).appendTo(params['divID']);
            //jQuery("#loeSummary").tmpl($obj).appendTo("#loe-summary");
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert('status Code:' + xhr.status + 'Error Message :' + thrownError);
        }
    });
}

How to use

1
2
3
4
5
6
7
8
9
10
params = {
    targetUrl: 'loe/getTestDetails/1/',
    action: 'loe_controller_getTestDetails',
    templateId: "#testDetails",
    divID: "#test-details",
    dataID: jQuery('#map-id').val()
}
//alert(jQuery('#active-team-id').val());

getDetails(params);
Did you enjoy this article? Share it!

About the Author:

Hi, This is Parvez Alam from India. I am software developer with 4 years’ experience in web development. I have submitted articles on PHP, Mysql, Magento,CSS, HTML, jQuery, web designing and social API. You can subscribe to my blog via RSS/Twitter/Google plus and Facebook. parvez1487(at)gmail(dot)com

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>