0

I'm using Symfony2.1 with Doctrine2.1

I'd like to use AJAX for many features on my site , editing a title , rate an article , create an entity on the fly , etc.

My question is simple :

Do I need to create a JQuery function for each functionnality , like this :

$('#specific-functionality').bind('click', function(e){

    var element = $(this);
    e.preventDefault();

    // the call
    $.ajax({
      url: element.attr('href'),
      cache: false,
      dataType: 'json',
      success: function(data){        

        // some custom stuff : remove a loader , show some value, change some css

      }
    });
  });

It sounds very heavy to me, so I was wondering if there's any framework on JS side, or a specific method I can use to avoid this. I was thinking about regrouping items by type of response (html_content , boolean, integer) but maybe something already exists to handle it nicely !

2 Answers 2

1

From what I understand, you are asking for lighter version of JQuery ajax method. There are direct get/post methods instead of using ajax.

 $.get(element.attr('href'), {'id': '123'}, 
    function(data) { 
       alert(data); 
    } 
 );

To configure error function

$.get(element.attr('href'), {'id': '123'}, function(data) {alert(data);})
.error(function (XMLHttpRequest, textStatus, errorThrown) {
                        var msg = jQuery.parseJSON(XMLHttpRequest.responseText);
                        alert(msg.Message);
});

Also, you can pass callback function to do any synchronous operations like

function LoadData(cb)
{
    $.get(element.attr('href'), { 'test': test }, cb);
}

And call

LoadData(function(data) { 
alert(data);
otherstatements; 
});

For progress bar, you use JQuery ajaxStart and ajaxStop functions instead of manually hiding and showing it. Note, it gets fired for every JQuery AJAX operation on the page.

 $('#progress')
   .ajaxStart(function () {
   //disable the submit button
   $(this).show();
   })
   .ajaxStop(function () {
   //enable the button
   $(this).hide();
   });
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of $('#specific-functionality').bind('click', function(e){, try this:

$(".ajax").click(function(){

    var url = $(this).attr("href") ;
    var target = $(this).attr("data-target") ;
    if (target=="undefined"){
      alert("You forgot the target");
      return false ;
    }

    $.ajax(....

And in html

<a class="ajax" href="..." data-target="#some_id">click here </a>

I think it is the simplest solution. If you want some link to work via ajax, just give it class "ajax" and put data-target to where it should output results. All custom stuff could be placed in these data-something properties.

1 Comment

Even better, thanks ! I'm looking at the vote system of StakOverflow, and it seems to be working with the same concept !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.