views:

89

answers:

3

I am using zend framework on windows. I want to implement ajax in my project first time. I searched for help and created a very simple ajax functionality.

IndexController.php

public function indexAction() {
}

public function oneAction() {
}

public function twoAction() {
}

index.phtml

<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="js/AJAX.js"></script>

<a href='http://practice.dev/index/one' class='one'>One</a>
<a href='http://practice.dev/index/two' class='two'>Two</a>
<br /><br />
<div id="one">one.phtml content comes here</div>
<div id="two">two.phtml content comes here</div>

AJAX.js

jQuery(document).ready(function(){
    jQuery('.one').click(loadOne);
    jQuery('.two').click(loadTwo);
});

function loadOne(event) {

    event.preventDefault();

    jQuery.ajax({
        url: '/index/one',
        success: function( data ) {
                    jQuery('#one').html(data);
                    }
    });
}

function loadTwo(event) {

    event.preventDefault();

    jQuery.ajax({
        url: '/index/two',
        success: function( data ){
                    jQuery('#two').html(data);
                    }
    });
}

Above code is working and loading data of one.phtml and two.phtml in 'one' and 'two' DIVs respectively when its link is clicked. You can see that I have to create separate jquery function for each link and also have to add new class for each link tag.

What I want to do ?:

I want to use only one jquery function for all AJAX requests and don't want to hard code url and success attributes in that function.

When I add "AJAX" class to any link tag then it should load content using AJAX.

Thanks.

A: 

Maybe this:

function loadData(url, callback) {
    jQuery.ajax({url: url, success: callback});
}

loadData('/index/one', function( data ) {
    jQuery('#one').html(data);
})

loadData('/index/two', function( data ) {
    jQuery('#two').html(data);
})

To make this even more compact you could define the same callback for both and then have the handler decide which element the response data should be written to.

Am
here #one and #two are also hardcoded. can we not also pass this? For example; array('one'=>one.phtml,'two'=>two.phtml);
NAVEED
Like I mentioned in the end of my reply, your response should contain something by which the Ajax event handler can tell what element it needs to apply the content of response to.
Am
+5  A: 

for simple loading of data in divs i would use the load method

HTML

<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="js/AJAX.js"></script>

<a href='http://practice.dev/index/one' class='ajax' rel="one">One</a>
<a href='http://practice.dev/index/two' class='ajax' rel="two">Two</a>
<br /><br />
<div id="one">one.phtml content comes here</div>
<div id="two">two.phtml content comes here</div>

JS

jQuery(document).ready(function(){
    jQuery('.ajax').click(function(event){
       event.preventDefault();

       var target = '#' + jQuery(this).attr('rel');
       var href = jQuery(this).attr('href');
       jQuery( target ).load( href );

      });
});

Use a single class to identify all links that should use ajax calls instead of their normal use. And add a rel attribute to those links that will contain the id of the container div..

Gaby
Darn. Got there before me.
Eric
Is that correct usage of the `rel` attribute, or would it make more sense to use the `data-` HTML5 attributes?
Eric
@Eric, you have a point. The usage i do is not the semantically intended by the w3 standards.. the `data-` would be more appropriate.. *although it would be invalid in non html 5 validators..*
Gaby
@gaby another possibility would be to put something like "target:one" or "target:two" into the "class" value. Then the script could just strip out the "one" or "two".
Pointy
@pointy, would that be considered valid html, having `:` in class names ? (*also, thanks for the edit..*)
Gaby
I don't think there are many rules about what's in class names, but you could always use something else, like "ajax-", as the prefix. I've never heard of any problems caused by funny characters in class names.
Pointy
@Pointy, indeed .. i just wondered about the `:` .. but validators are fine with it.
Gaby
Thanks. Its working but one problem still exists. When I click any link, it is not changing url in browser's address bar. Anyway its good for me. I am learning AJAX step by step and its good step no.2 for me. My next step will be json and ajax combination.
NAVEED
@Naveed, have a look at this SO question/answer as it addresses the issue and a potential pitfall (*scrolling*) [Modifying document.location.hash without page scrolling](http://stackoverflow.com/questions/1489624/modifying-document-location-hash-without-page-scrolling)
Gaby
A: 

Compact version:

$(function(){
    $('.one').click(loadOne);
    $('.two').click(loadTwo);
});

function loadOne(event) {
    event.preventDefault();
    $('#one').load('/index/one');
}

function loadTwo(event) {
    event.preventDefault();
    $('#two').load('/index/two');
}
Eric