SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    best way to plan for AJAX?

    i'm making my site, and i want to do just the essentials and leave the eye candy for AFTER i launch, meaning i want to do normal calls now, but be able to have nice AJAX features later. also this is for backwards compatibility.

    for example, i am making one of my 'settings' pages right now. i have 3 available states of the form: 'default', 'submit', and 'complete'.

    1. 'default' is the page with the forms for them to fill out, etc.
    2. 'submit' is for after they have submitted the page...it verifies the form contents and saves to the database, then forwards to the third state, which is 'complete'
    3. 'complete' is only a confirmation message i.e. "your settings have been saved"


    this part of the application is mainly procedural. i would like to make sure that in the future i can easily add AJAX capabilities without duplicating code and thus making maintainability a nightmare. the AJAX feature that i was focusing on would be the ability to click the 'submit' button, and have it just popup 'settings saved'...so there effectively wouldn't be a 'complete' state anymore. what would be the best way to do this?

    really all i'm asking is what is the best way to setup this page so i can reuse the code in 'submit' (which verifies the submitted information) in a different context. should i change this procedural code into a class, since one of the major advantages of OOP is the reusablity? if so...could someone quickly outline how i should design that? if OOP isn't the best way...what is?

    in all reality i could do this myself...but i'm so new to AJAX i'm sure some of you experts have found some great techniques to allow your apps to be more AJAX friendly.

    thanks.

    forgive me if this is the wrong place for this question, wasn't sure.

  2. #2
    SitePoint Zealot Mau's Avatar
    Join Date
    Jan 2006
    Location
    California, USA
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,
    Consider separating your presentation out (perhaps MVC?). This way you can have your business logic for the request that runs in both ajax and old school mode. But, depending on whether the request is ajax or not, you use a different presentation. For "old school mode", your presentation spits out X/HTML, while in ajax, it can spit out JSON.

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your reply,

    i was under the impression that i was separating my presentation from business logic already, but i'm beginning to think i really haven't done that. what i do is have a page that does all the work and creates a collection (array) of variables, then simply passes those to a template with placeholders for those variables.

    maybe it's only a matter of creating a new condition of if (ajax){output to ajax template} else {output to html template }. i can't tell...i'm just very confused as to how i should even start this or if i'm already prepared for what i'm needing and don't even know it.

    maybe it'll help to have some code (mods if this is becoming a topic for the 'PHP' forum, feel free to move)
    PHP Code:
    switch ($action){
        case 
    'complete':
            
    // echo "settings saved" message
            
    break;
            
        case 
    'submit':
            
    // parse variables
            
    if (!errors){
                
    // save into database
                // forward to $action=complete    
            
    } else {
                
    // echo forms (and display errors)
            
    }
            break;
            
        case 
    'default':
        default:
            
    // echo forms
            
    break;

    the preceding is the logic i'm using currently. i could just add extra if(ajax) statement...but it seems like that will only prepare me for this one instance of ajax (and it will double the number of template pages i have, one for HTML and one for AJAX). how will i know if the current code i'm writing is ready for all future upgrades to different output techniques?

    i've been searching for the past hour since i first posted, and i'm beginning to think design patterns are the way to go. I'm checking out MVC as you suggested now...maybe that can get me where I'm trying to go. as it is now, design patterns are pretty much nonexistent in my app.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •