I am building my first rails app with backbone and because of the asset pipeline the Javascript is getting called/executed even before the document fully loads. So, none of the event handlers are getting attached. If I place this Javascript after the HTML tags at the end of the document, then it seems to work fine. How can I have this code execute after the page is fully loaded? I can always use jQuery's document.ready(), but I was hoping backbone has an inbuilt process to deal with it.
<script type="text/javascript">
(function() {
"use strict";
var app;
app = {};
app.AppView = Backbone.View.extend({
el: "body",
initialize: function() {
this.playAudio();
},
events: {
"click .play-audio": "playAudio"
},
playAudio: function() {
alert($("span").data("audio"));
}
});
app.appView = new app.AppView();
}).call(this);
</script>
<div>
<p>Whatever!</p><span class="glyphicon glyphicon-volume-up play-audio" data-audio="http://my-audio-file"></span>
</div>
Assign your function as document.onload or window.onload property. Both does almost the same thing but mostly depends on browser.
<script> document.onload=(...your function goes here....) </script>
//OR
<script> window.onload=(...your function goes here....) </script>