Welcome Guest, Not a member yet? Register   Sign In
CI with Jquery/Ajax best practices
#1

[eluser]blzabub[/eluser]
Hello,

I'm just getting into codeigniter and really enjoying it.

I'm building a calendar administration system using the calendar library. I'd like to use jquery's ajax features to add/edit calendar entries. I've had some problems where the URL referenced by my jquery code (in a separate javascript file), needs some awareness of the current URL which varies based on what year and month of the calendar the user is viewing, for example-

application/admin/calendars/index/2010/05 (May 2010)

or

application/admin/calendars/index/2011/12 (December 2011)

My jquery code can be aware of this by use of base_url or site_url I suppose, but then I would have to embed the jquery code into my php file (CI view file) so that the base_url() function could be called. Is this really the best way to accomplish this? It seems like having jquery code in my CI view file violates some guidelines about unobtrusive javascript, etc. I was wondering if anyone was familiar with a better methodology for doing this.

Thanks in advance,

~Ben
#2

[eluser]BradEstey[/eluser]
[quote author="blzabub" date="1272057404"]
My jquery code can be aware of this by use of base_url or site_url I suppose, but then I would have to embed the jquery code into my php file (CI view file) so that the base_url() function could be called. Is this really the best way to accomplish this? It seems like having jquery code in my CI view file violates some guidelines about unobtrusive javascript, etc. I was wondering if anyone was familiar with a better methodology for doing this.
[/quote]

All you need is like one line of jQuery code on your page calling a function that passes your variable (whether it's base_url or site_url) to the external jQuery script file containing the function. If I had more information information I could give you some exact code to use. But in most cases, you should be able to wrap your jQuery code in a function in order to pass variables to it.

Or another option would be to use Sessions of Cookies that both jQuery and PHP can access and modify independently, but that's a lot more complicated.
#3

[eluser]blzabub[/eluser]
Ah...okay, interesting idea.

What about avoiding PHP/CI completely and using javascript to parse the current URL? I know my original question did not frame it as such but how about just using [removed] to find the URL?
#4

[eluser]blzabub[/eluser]
Just tried passing the base_url to my jquery script by adding a single line of javascript with a global variable in it. Seems to work just fine. Thanks for your help Bradillac!
#5

[eluser]blzabub[/eluser]
Anybody with thoughts on using the HTML base tag to solve this problem?
#6

[eluser]Tominator[/eluser]
<base href="http://yoursite.com">

But I really don't understand your problem. Can you describe it better?

Tom.
#7

[eluser]vitoco[/eluser]
i think this may work

In the view
Code:
<!-- add 3 navigation links ( #prev , #current , #next ) -->
<a id="prev_month" href="&lt;?=base_url().'admin/calendars/index/2010/05'?&gt;">Prev</a>
<a id="current_month" href="&lt;?=base_url().'admin/calendars/index/2010/06'?&gt;">June 2010</a>
<a id="next_month" href="&lt;?=base_url().'admin/calendars/index/2010/07'?&gt;">Next</a>
In the js file
Code:
var current_url = '' ;
$(document).ready( function()
{
    current_url = $('a#current_month').attr('href');
    //
    // USE current_url + params to ajax calls
});

Saludos
#8

[eluser]blzabub[/eluser]
Hi Everyone,

Thanks for the suggestions. I've decided to use HTML &lt;base&gt; base tag as my working solution. I was really asking for advice on what the best practices method was, but along the way I received many interesting suggestions, thanks for your help.

~Ben
#9

[eluser]pickupman[/eluser]
Your js files can also have a php file extension, and use both js & php.




Theme © iAndrew 2016 - Forum software by © MyBB