Updating UI realtime in RoR after API Calls
I am developing an app using the Amazon API found here, which unfortunately only allows a small amount of records to be returned at once, which causes a significant slowdown when loading any more than one call's worth of items. I'd like to make the returni开发者_JAVA百科ng of these records more seemless. I figure the way to do it, is to use something Node, or some other asynchronous calls, but am having some trouble implementing the idea.
Any help would be much appreciated.
You should be able to do what you need with a more robust client-side JavaScript library. jQuery has a respectable AJAX driver which is a great place to start.
If your Rails app takes some time to produce results because of a dependency on the Amazon API, split the page rendering into two components. Render the page immediately with a JavaScript trigger to dynamically load the results of your call, and this call can "spin" for a moment before the HTML is inserted in the document.
This works even better if the operation to trigger the load is AJAX to start with, as you won't need to render that "in progress" page as a separate step.
Although you can use Node.js for this sort of thing as a back-end intermediation tool, you may find it's easier to work with EventMachine as it's built with Ruby and you can leverage a lot of your Rails code directly.
精彩评论