ruby on rails - how to specify the view or controller for running javascript 'document.ready' -
i want run javascript bellow when page loaded.
$ -> $.getjson '/memos.json', (data) -> $.each( data ), (key,memo)-> $('#memos').append( "#{memo.title} #{memo.content}<br/>")
but want run after 'users/show.html.erb' loaded. view bellow
<div id="memos"></div>
i think there 2 approaches this. 1 specify views loaded, , second 1 specify controller.
i don't think writing javascript on erb file idea. there way implement them?
thanks.
the
$ -> ..
on first line, translated
$(function() { ... });
which shorthand jquery's
$(document).ready(function() { ... });
which means script inside of function executed once dom ready.
so save javascript separate file let js/memos/index.js
, within view app/views/memo/index.html.erb' have the
` , on same file can include javascript javascript include tag.
the javascript executed once dom ready, meaning @ point have div.
more info on jquery's ready
http://api.jquery.com/ready/
Comments
Post a Comment