Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3974794
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:36:35+00:00 2026-05-20T04:36:35+00:00

I have written a function that retrieves a html template, then binds data using

  • 0

I have written a function that retrieves a html template, then binds data using jQuery.tmpl. I think it’s fairly neat and tidy and encapsulates what I need and provides me a reusable function. My question however is can it be improved.

My main concern is what if the $.get method fails, and also how the callBack function is executed.

function Bind(templateURL, templateData, templateTarget, callBack){
var req = $.get(templateURL);
    req.success(function(templateHtml) { 
        $(templateTarget).html(''); //clear
        $(templateHtml).tmpl(templateData).appendTo(templateTarget); //add deal
    callBack();
    });
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:36:35+00:00Added an answer on May 20, 2026 at 4:36 am

    You can pass the result of tmpl() directly to html() to clear your target container and append the new content at the same time. You can also chain the result of $.get() into your success handler to avoid using a local variable:

    function Bind(templateURL, templateData, templateTarget, callBack)
    {
        $.get(templateURL).success(function(templateHtml) { 
            $(templateTarget).html($(templateHtml).tmpl(templateData));
            callBack();
        });
    }
    

    If $.get() fails, nothing will happen since you do not register an error handler. What that handler would do is up to you, but you might want to display an appropriate message in an alert box or somewhere on the page.

    Your second concern is less clear. As it stands, callBack will only be called on success, and without arguments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a function that gets a given number of random records from
I have a function I've written that was initially supposed to take a string
To dynamically fill DropDown controls on my HTML Form, I have written code that
I have a simple function written in Oracle 9i (version 9.2.0.4.0) to simulate an
I have written an AIR Application that downloads videos and documents from a server.
I have written some code in my VB.NET application to send an HTML e-mail
I have written a site in Prototype but want to switch to jQuery. Any
I have written a DLL that uses MS Word to spell check the content
I have an HTML page that uses AJAX to retrieve messages from a server.
I've got data in SQL Server 2005 that contains HTML tags and I'd like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.