I share with the audience a
small plugin written the day before that can be useful to you. There is nothing to add to the description on the githaba, so I will simply retell it.
The plugin allows to transfer DOM manipulation commands in one object, that is, to replace many method calls with a single function call.
Using
$.executeObject({"#my_div": ["text", " "]})
Why is this?
It is not easy to debug javascript transmitted from the server, so it is better to transfer the object and minimize the code. Instead
')
$('table tr:first').remove(); $('input#name').val('John'); $('#my_div').addClass('error').text(' ');
You can create an object and pass it to the executeObject function:
var = { 'table tr:first': 'remove' , 'input#name': ['val', ''] , '#my_div': [ ['addClass', 'error'] , ['text', ' '] ] } $.executeObject()
In particular, if you have accumulated something ugly in the form of templates on the ERB:
$('input#name').val('<%= escape_javascript(@person.name) %>') $('#my_div').addClass('error').text('<%= escape_javascript(@error) %>')
You can arrange it in the controller (in the example Ruby):
obj = {'input#name' => ['val', @person.name], '#my_div' => [%w[addClass error], ['text', @error]]} render js: "$.executeObject( #{obj.to_json} )"
and the template (view) generally throw. You can also specify $ .executeObject as an Ajax callback:
$.post('/my/url', {: ''}, $.executeObject)
and in response simply to issue the prepared object:
render json: obj
In other words, it is not necessary to transfer executable code where data can be transmitted.
Return value
The function will return the number of successful calls. Unsuccessful calls are silently ignored.