Be Excellent To Each Other

And, you know, party on. Dude.

All times are UTC [ DST ]




Reply to topic  [ 23 posts ] 
Author Message
 Post subject: Programming stuff
PostPosted: Wed Feb 23, 2011 15:34 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69725
Location: Your Mum
Hello, geeks!
I'd like a quick discussion about how you do your AJAX (looks pointedly at the topic icon (here it is again, phone people: Image)).

I use PHP and jQuery, so I've got the main PHP file, and the JS file is called from that. Any AJAX calls I make go to a file called [mainfilename]_ajax.php (which normally contains a big switch on some passed-through variable to decide what to do), but lately I've been throwing them at the start of the main PHP file itself, and then exciting before the rest of the page is run if it was, indeed, an AJAX call.
Neither of these methods feel quite 'right', though.

What do you do?

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: AJAX file organisation
PostPosted: Wed Feb 23, 2011 15:44 
User avatar
Comfortably Dumb

Joined: 30th Mar, 2008
Posts: 12034
Location: Sunny Stoke
I use the former method, but only because that's how I'd seen it done elsewhere so I tweaked the code to suit my needs, hence the external ajax file. To me it seems the neater way of doing it than your other method but I don't know if there's any real difference. :shrug:

_________________
Consolemad | Under Logic
Curse, the day is long
Realise you don't belong


Top
 Profile  
 
 Post subject: Re: AJAX file organisation
PostPosted: Wed Feb 23, 2011 15:46 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69725
Location: Your Mum
Examples!

Old way:
Code:
<?php
// mainfile.php

require_once ("common.php");

include("page_head.php"); // this calls the JS scripts

//blah blah
?>

Code:
//javascript file
$(document).ready(function() {
$.ajax({
   type: "POST",
   url: "mainfile_ajax.php",
   data: {
      "func": "do_someting",
      "someid": an_id
   },
   success: function(ret) {
      $("#dialog_box").html(ret);
   }
});
});

Code:
<?php
//mainfile_ajax.php

require_once ("common.php");

$func = $_POST['func'];

switch ($func) {
    case 'do_something' :
        $id = $_POST['some_id'];
        print "Something done!";
    break;
}


//blah blah
?>


New way does away with mainfile_ajax.php, and changes mainfile.php to look like this:

Code:
<?php
//mainfile_ajax.php

require_once ("common.php");

$func = $_POST['func'];

switch ($func) {
    case 'do_something' :
        $id = $_POST['some_id'];
        print "Something done!";
    break;
}

if ($func) {
    exit;
}

include("page_head.php"); // this calls the JS scripts

//rest of mainfile.php
?>

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: AJAX file organisation
PostPosted: Wed Feb 23, 2011 15:51 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69725
Location: Your Mum
devilman wrote:
I use the former method, but only because that's how I'd seen it done elsewhere so I tweaked the code to suit my needs, hence the external ajax file. To me it seems the neater way of doing it than your other method but I don't know if there's any real difference. :shrug:

It depends on how you term "neat", I guess - the code inside the files is neater but the file layout gets more cluttered. Not so much of a problem with good IDEs but it's something that niggles at me.

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: AJAX file organisation
PostPosted: Wed Feb 23, 2011 16:05 
User avatar

Joined: 30th Mar, 2008
Posts: 14380
Location: Shropshire, UK
I don't use a separate file for AJAX, I just have it included in the same file as the one I'm working on, and get the AJAX request from jQuery to have an "&ajax=1" parameter in it, where I check for it and run it accordingly, exiting the script before outputting the non-AJAX stuff.

Much neater IMO, less files to copy so if you want to re-use it elsewhere there's less things to remember.


Top
 Profile  
 
 Post subject: Re: AJAX file organisation
PostPosted: Wed Feb 23, 2011 17:00 
User avatar
Sitting balls-back folder

Joined: 30th Mar, 2008
Posts: 10175
I'd probably not just do the first thing, I'd possibly have a separate PHP for each function, pulling in common routines script(s). Depends on function-unique complexity. But then I'm a C++ programmer* who doesn't have to worry about interpretation/JIT compilation and caching, and I haven't written PERL/PHP for ages. Though coincidentally, it looks like I'll be writing some PERL to help with MissN's job this week.

* I like polymorphism and templates and everything.


Top
 Profile  
 
 Post subject: Re: AJAX file organisation
PostPosted: Wed Feb 23, 2011 17:08 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69725
Location: Your Mum
BikNorton wrote:
I'd probably not just do the first thing, I'd possibly have a separate PHP for each function, pulling in common routines script(s).

Christ, really?
I guess that would make a small difference to overhead each time there was an AJAX call (unless you cached everything), but organizationally it'd be a bit of a nightmare.

For example, just the "game" bit of my Space Cows game would need 133 separate PHP files for AJAX :S

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: AJAX file organisation
PostPosted: Wed Feb 23, 2011 17:14 
User avatar
Sitting balls-back folder

Joined: 30th Mar, 2008
Posts: 10175
Without knowing what the functions are/do, I can't say. I might break it down to several related functions in each top-level script. Depends on the project's characteristics, dunnit.

Likewise I might have one massive class, or break out specialised functionality into discrete classes. All down to ease of visualisation and maintenance. :shrug:


Top
 Profile  
 
 Post subject: Re: AJAX file organisation
PostPosted: Wed Feb 23, 2011 17:15 
User avatar
Comfortably Dumb

Joined: 30th Mar, 2008
Posts: 12034
Location: Sunny Stoke
Most of my coding work has been very small scale, so I don't really mind having an extra file here and there. Because I'm very much an amateur at this, I do wonder why my code is a fraction of the size of 'proper' stuff and with a fraction of the number of files too. For example, I've just downloaded a 1.6 installation of Joomla and that consists of over 4,000 files. OrangeHRM is about 1,000 files too. I couldn't imagine keeping track of that kind of stuff.

_________________
Consolemad | Under Logic
Curse, the day is long
Realise you don't belong


Top
 Profile  
 
 Post subject: Re: AJAX file organisation
PostPosted: Wed Feb 23, 2011 17:16 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69725
Location: Your Mum
That's what your IDE is for. What do you do your coding in?

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: AJAX file organisation
PostPosted: Wed Feb 23, 2011 17:17 
User avatar
Comfortably Dumb

Joined: 30th Mar, 2008
Posts: 12034
Location: Sunny Stoke
Grim... wrote:
That's what your IDE is for. What do you do your coding in?


Just Notepad++.

_________________
Consolemad | Under Logic
Curse, the day is long
Realise you don't belong


Top
 Profile  
 
 Post subject: Re: AJAX file organisation
PostPosted: Wed Feb 23, 2011 17:18 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69725
Location: Your Mum
Something like PHPEd (for PHP) would keep track of all that for you.
But some things (like Joomla) do seem rather overkill, I agree.

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: AJAX file organisation
PostPosted: Wed Feb 23, 2011 17:31 
User avatar
Comfortably Dumb

Joined: 30th Mar, 2008
Posts: 12034
Location: Sunny Stoke
Grim... wrote:
Something like PHPEd (for PHP) would keep track of all that for you.


I'll have to give that a try as I want to learn more PHP so anything that makes my coding a little less amateurish then all the better. ;) Although saying that, one of our clients recently renewed but only for the product which I developed - for £1500 + £250 a year maintenance which was a bit 8)

_________________
Consolemad | Under Logic
Curse, the day is long
Realise you don't belong


Top
 Profile  
 
 Post subject: Re: AJAX file organisation
PostPosted: Wed Feb 23, 2011 17:52 
User avatar

Joined: 23rd Nov, 2008
Posts: 9521
Location: The Golden Country
I always thought AJAX was a bog cleaner.



I'll get my coat.

_________________
Beware of gavia articulata oculos...

Dr Lave wrote:
Of course, he's normally wrong but interestingly wrong :p


Top
 Profile  
 
 Post subject: Re: AJAX file organisation
PostPosted: Wed Feb 23, 2011 17:54 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69725
Location: Your Mum
devilman wrote:
Grim... wrote:
Something like PHPEd (for PHP) would keep track of all that for you.


I'll have to give that a try as I want to learn more PHP so anything that makes my coding a little less amateurish then all the better. ;) Although saying that, one of our clients recently renewed but only for the product which I developed - for £1500 + £250 a year maintenance which was a bit 8)

They do a trial version, I think.
And they do a "trial version".

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: AJAX file organisation
PostPosted: Wed Jun 08, 2011 12:15 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69725
Location: Your Mum
Grim... wrote:
Something like PHPEd (for PHP) would keep track of all that for you.

Hello, PHP programmers!
You might be interested to know that PHPEd 6 is out, and it finally has intergrated JavaScript support, as well as being generally faster.

Demo-you-do: http://www.nusphere.com/download.php.ide.htm

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: AJAX file organisation
PostPosted: Wed Jun 08, 2011 14:41 
User avatar
Participant in dramatic games

Joined: 30th Mar, 2008
Posts: 4151
Location: United Provinces
lalalalalalala ajax amsterdam...

sorry

_________________
XBL: Romanista WiiU: Romanista77 Gamecenter: Romanista345 3DS 0318 8943 6467
Steam: Romanista345 PSN: Romanista345 Switch: 5098 6135 1325 RetroAchievements: Romanista
[img=500x70]https://card.exophase.com/2/0/252928.png?1717524262[/IMG]


Top
 Profile  
 
 Post subject: Re: AJAX file organisation
PostPosted: Fri Nov 25, 2011 11:44 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69725
Location: Your Mum
This thread'll do.

The new version of Tortoise SVN only stores things in one folder the root of your working copy, rather than every folder. Hurrah!

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: Programming stuff
PostPosted: Fri Nov 25, 2011 12:15 
User avatar

Joined: 30th Mar, 2008
Posts: 14380
Location: Shropshire, UK
Oooh. About bloody time.


Top
 Profile  
 
 Post subject: Re: Programming stuff
PostPosted: Fri Nov 25, 2011 12:20 
User avatar
Heavy Metal Tough Guy

Joined: 31st Mar, 2008
Posts: 6614
Use git!


Top
 Profile  
 
 Post subject: Re: Programming stuff
PostPosted: Fri Nov 25, 2011 12:35 
User avatar
ugvm'er at heart...

Joined: 4th Mar, 2010
Posts: 22397
Ironically Git is the least gitty out of all of them :D


Top
 Profile  
 
 Post subject: Re: Programming stuff
PostPosted: Fri Nov 25, 2011 12:38 
User avatar
Heavy Metal Tough Guy

Joined: 31st Mar, 2008
Posts: 6614
We used to use CVS on a pretty hefty code base - removing and then creating a tag would takes AGES, and would gum up the server whilst you did it. Git makes branching and tagging sooooo easy and quick. We do it just for fun now!


Top
 Profile  
 
 Post subject: Programming stuff
PostPosted: Fri Nov 25, 2011 13:23 
User avatar
baron of techno

Joined: 30th Mar, 2008
Posts: 24136
Location: fife
I fucking hate git. I want my WinCVS back ;(


Top
 Profile  
 
Display posts from previous:  Sort by  
Reply to topic  [ 23 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Satsuma and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search within this thread:
You are using the 'Ted' forum. Bill doesn't really exist any more. Bogus!
Want to help out with the hosting / advertising costs? That's very nice of you.
Are you on a mobile phone? Try http://beex.co.uk/m/
RIP, Owen. RIP, MrC. RIP, Dimmers.

Powered by a very Grim... version of phpBB © 2000, 2002, 2005, 2007 phpBB Group.