ruby on rails - javascript code or json objects as ajax response? -
I am reading railway guides, especially I am reading this: The part which is a bit confusing to me. It says that if Action Actions receives an AJAX request, it will return the JavaScript code which will include the user in the page. I have found other solutions in which the client gets the json object (instead of the JavaScript code) and creates the respective DOM elements and inserts them on the page. My questions are:
- What is the json object? Receive and create DOM elements and get them better than receiving the JavaScript code on the page and executing it so that the new content is added to the page? What are the best practices about this?
- In this example, what will get the AJAX call? Javascript code, a Jason object or both?
There is no right or wrong way in the example that you have quoted, Ajax calls to Javascript will be received, which will then be executed in the browser. However, it is likely that you want to embed html in javascript feedback. For example, a rail part has been partly responded in:
Controller: def create user = user.new (: user_params) @ user.save response_to do | Format | ('& Lt;% = render (partial:' user_row ')%> gt; partial: _user_row.html.erb & lt; Div class = "user line" & gt; & lt;% = @ user.name% & gt; & Lt; / Div & gt;
Parts will access variables in part to form a controller. If you want to insert, it is big or complex, it will be able to simplify your code and make it more readable.
To do the same thing with answering JSON, as you say, you must manually construct the DOM element if you were limited to server resource then manually making DOM element , Or if you prefer to limit the amount of data you send to the network. Note that when you react with JSSN, most of the work is done in the browser and you only have to send Jason object, with embedded HTML with Javascript.
Comments
Post a Comment