2

Short version: Is there a callback when the rails view is finished rendering?

Longer explanation of my actual problem:

I have this issue where I want to generate a history-object because of some APIs I'm using. So I will be appending to this history object possibly several times before saving it. I know that I can safely save it when the view is rendered, because then there will not be any more calls to the APIs.

After quite a while of googling I can still not find any way of doing this. Is my intended approach the right one, to get a callback from the renderer that says "now everything is rendered. Go ahead and save", or should I do it in a different way?

Does such a callback even exist, or do I need to make it myself?

4

1 回答 1

3

I would lean towards suggesting that you shouldn't be leaving functionality until after the page has rendered - you should really have completed any API calls in the controller before you start outputting the page. However, if you really must do this you can use javascript to fire off a function (such as an ajax request to an action) after a page has finished rendering.

http://api.jquery.com/ready/ should give you a good starting point!

于 2013-07-18T23:18:03.697 回答