7 Replies Latest reply on Apr 19, 2010 8:59 AM by Gabriel Landes

    Instant Ajax response?

    lovewebdev Level 1

      I'm pretty good with CF and Ajax but I'm struggling over the idea of a browser page getting GET or POST action from another page and having the currenty page instantly load it without any action from the user on that page.

       

      I know how to load ajax with user interaction on the viewed page but what I mean is something like Google's Twitter results scrolling down with no action from the user directly from the viewed page.

       

      Is it a matter of logging the GET/POST action into the database and then using something like Javascript/Ajax setinterval to reload data every few seconds and read the database until it finds something?

        • 1. Re: Instant Ajax response?
          Dan Bracuk Level 5

          Your concept seems ok.  Is there any part you are having difficulty with?

          • 2. Re: Instant Ajax response?
            lovewebdev Level 1

            I dont have a major problem with that. i was just wodering if that was an efficient way. From first glance, it seems using setinterval to continously read from a database in a multiuser environment might be too much. I can play with that. Do you know of another way?

            • 3. Re: Instant Ajax response?
              lovewebdev Level 1

              The other thing is I don't like the way the data reloads. When I use an ajax call and put the return into an html span using setinterval to keep reloading the data.

               

              All the data refreshes itself, going black and then coming back. It doesnt stack or update smoothly, which is why I'm wondering if it's really the right way.

              • 4. Re: Instant Ajax response?
                Gabriel Landes

                I'm a bit of a newb to this area, but the answer likely lies in how you are returning the data to the screen. If you bind the data to a container and refresh the whole container, you'll get the behavior you described. Instead need to look into creating a new container for each new item. This would mean manipulating the DOM to create the new node and then bringing it in gracefully.

                 

                I wish I could give you an example in code, but I don't have one. I have been learning jQuery lately though and it seems very suited to making this rather easy.

                 

                Just a thought...

                • 5. Re: Instant Ajax response?
                  lovewebdev Level 1

                  No I kind of see what you're saying actually. I'll have to play with it and see how to create a new <span> for each return.

                   

                  Also, does anyone know how to redirect the page based on what's returned to a <span> through an Ajax call.The problem is that data is constantly being returned using setinterval so I can't use a redirect javascript when the page loads because the data might not have bene fetched so the script won't do anything.

                  • 6. Re: Instant Ajax response?
                    TLC-IT Level 3

                    The "gotcha" here is that you're trying to work within the ColdFusion framework's way of doing things, without "stepping out of it" to do straight JavaScript coding.  (And I frankly recommend that you probably shouldn't "step out of it."  That's what CF is for.)

                     

                    Be careful that you do not wind up spending an unjustifiable amount of time trying to "make it work."

                    • 7. Re: Instant Ajax response?
                      Gabriel Landes Level 1

                      While I definitely agree with the advice to be careful of being time conscious, I might respectfully suggest that CF and many manner of JS can coexist very peacefully and yield some real efficiency benefits.

                       

                      While Coldfusion offers some great tools, there are some things that it doesn't offer much of, like effects (scriptaculous and jQuery are stronger here) . I've also noticed that sometimes the CF solutions can become cumbersome without some custom JS work to streamline it. We had a page once with a large table of data where each cell was initially set up using a CF Ajax tool. When the data set go large though, we found that CF's autogenerated JS code was growing to large for a reasonable page load time - it was creating a duplicate function for every cell. The solution was to use our own code to handle the cells and reuse a single instance of the CF functionality.

                       

                      This may not have been outside what you suggested not "stepping out", but in any case, we have had really good results combining CF's built-in JS tools with other libraries like jQuery (currently my favorite) and Scriptaculous. CF offers a lot and we certainly make use of it (I love the 'tabbed' layouts, cfdiv,cfform,etc. ), but there are lots of tools.

                       

                      We use them all with the obvious caveat that you do need to be judicious in selecting tools. You do need to be careful to use (load) only what you need.
                      Anyway...