{"id":6069,"date":"2014-04-12T02:59:17","date_gmt":"2014-04-12T02:59:17","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/12\/back-navigation-not-reloading-jwplayer-in-backbone-js-and-rails-3-app-collection-of-common-programming-errors-2\/"},"modified":"2014-04-12T02:59:17","modified_gmt":"2014-04-12T02:59:17","slug":"back-navigation-not-reloading-jwplayer-in-backbone-js-and-rails-3-app-collection-of-common-programming-errors-2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/12\/back-navigation-not-reloading-jwplayer-in-backbone-js-and-rails-3-app-collection-of-common-programming-errors-2\/","title":{"rendered":"Back navigation not reloading jwplayer in backbone js and rails 3 app-Collection of common programming errors"},"content":{"rendered":"<p>My app is a rails 3 app using backbone.js and jw player for playing a playlist of videos. The index_view for the videos has all the videos loaded into a playlist for jw player. My problem comes when I navigate away from the index page, let&#8217;s say to the show_view for an individual video. When I click the browser&#8217;s back button I get an error when the jw player tries to load again.<\/p>\n<pre><code>Uncaught TypeError: Object # has no method 'setup' \n<\/code><\/pre>\n<p>I believe the problem is that the template hasn&#8217;t loaded the html element that the player uses to instantiate itself. That&#8217;s my current theory. If you look in <code>index_view.js.coffee<\/code> you see that in the <code>render<\/code> method I use the JQuery <code>$(document).read -&gt;<\/code> method to load the player. If I remove that, the player doesn&#8217;t load and I see the same error.<\/p>\n<pre><code>Uncaught TypeError: Object # has no method 'setup' \n<\/code><\/pre>\n<p>This error can be reproduced simply by calling the jwplayer on a non-existant CSS id. ie <code>jwplayer('non-existant-id').setup(...)<\/code> would produce the same error. I&#8217;m pretty new to backbone but I would assume that the JQuery document.ready method has no effect after the initial loading of the index page. The DOM is never reloaded once I&#8217;m using the <code>#<\/code> routes, so when I navigate back to the index page, the id &#8216;my-video&#8217; doesn&#8217;t yet exist so calling <code>jwplayer('my-video')<\/code> produces an error. Is there any sort of <code>Backbone.ready<\/code> method? \ud83d\ude42<\/p>\n<p>So here&#8217;s some code, lemme know if you need anything else:<\/p>\n<p><code>index.html.erb<\/code> The rails view for videos<\/p>\n<pre><code>\n\n\n  $(function() {\n    \/\/ Blog is the app name\n    window.router = new SeehearmeWebapp.Routers.VideosRouter({videos: , users: , questions: });\n    Backbone.history.start();\n  });\n\n<\/code><\/pre>\n<p><code>videos_router.js.coffee<\/code><\/p>\n<pre><code>class SeehearmeWebapp.Routers.VideosRouter extends Backbone.Router\n  initialize: (options) -&gt;\n    @videos = new SeehearmeWebapp.Collections.VideosCollection()\n    @videos.reset options.videos\n    @users = new SeehearmeWebapp.Collections.UsersCollection()\n    @users.reset options.users\n    @playlist = []\n    @questions = options.questions\n    for i in [0..@videos.length-1]\n      video = @videos.models[i]\n      versions = video.attributes.versions[6]\n      images = video.attributes.thumbnails[0]\n      question = @questions[parseInt(video.attributes.question_id)-1]\n      if !(question == undefined)\n        title = question.text\n      else\n        title = \"\"\n\n      if !(versions == undefined)\n        creator_id = video.attributes.creator_id.toString()\n        @playlist.push {file: versions.url, creator_id: creator_id, gender: @users.get(creator_id).attributes.gender, question: title ,  image: images.url}\n\n  routes:\n    \"new\"      : \"newVideo\"\n    \"index\"    : \"index\"\n    \":id\/edit\" : \"edit\"\n    \":id\"      : \"show\"\n    \".*\"        : \"index\"\n\n  newVideo: -&gt;\n    @view = new SeehearmeWebapp.Views.Videos.NewView(collection: @videos)\n    $(\"#videos\").html(@view.render().el)\n\n  index: -&gt;\n    @view = new SeehearmeWebapp.Views.Videos.IndexView(videos: @videos, users: @users, playlist: @playlist)\n    $(\"#videos\").html(@view.render().el)\n\n  show: (id) -&gt;\n    video = @videos.get(id)\n\n    @view = new SeehearmeWebapp.Views.Videos.ShowView(model: video)\n    $(\"#videos\").html(@view.render().el)\n\n  edit: (id) -&gt;\n    video = @videos.get(id)\n\n    @view = new SeehearmeWebapp.Views.Videos.EditView(model: video)\n    $(\"#videos\").html(@view.render().el)\n<\/code><\/pre>\n<p><code>index_view.js.coffee<\/code><\/p>\n<pre><code>SeehearmeWebapp.Views.Videos ||= {}\n\nclass SeehearmeWebapp.Views.Videos.IndexView extends Backbone.View\n  template: JST[\"backbone\/templates\/videos\/index\"]\n\n  playerHeight = '360'\n  playerWidth = '640'\n  defaultVersion = 0\n  playlist = []\n\n  initialize: () -&gt;\n    @options.videos.bind('reset', @addAll)\n    playlist = @options.playlist\n\n  addAll: () =&gt;\n    @options.videos.each(@addOne)\n\n  addOne: (video) =&gt;\n    view = new SeehearmeWebapp.Views.Videos.VideoView({model : video})\n    @$(\"tbody\").append(view.render().el)\n\n  render: =&gt;\n    $(@el).html(@template(videos: @options.videos.toJSON() ))\n    @addAll()\n\n    $(document).ready -&gt;\n      player = jwplayer('my-video')\n      player.setup({playlist: playlist,  width: playerWidth, height: playerHeight, skin: \"\/jwplayer\/skins\/six\/six.xml\"})\n\n    return this\n<\/code><\/pre>\n<p><code>index.jst.ejs<\/code><\/p>\n<pre><code>\n    \n        Show Me: \n        \n            Women and Men\n            \n                <img decoding=\"async\" src=\"assets\/down-arrow.png\" \/>\n            \n            <\/code><\/pre>\n<ul class=\"dropdown-menu gender-dropdown-menu\">\n<li><code>Women<\/code><\/li>\n<li><code>Men<\/code><\/li>\n<li><code>Women and Men<\/code><\/li>\n<\/ul>\n<pre>\n        \n    \n    \n    <\/pre>\n<h2 id=\"question-text\" class=\"white centered-text\"><code>seehear.me<\/code><\/h2>\n<pre>\n    \n    \n    \n        \n        meet her\n        \n    \n\n<br \/>\n<\/pre>\n<p><code><code>show_view.js.coffee<\/code><\/code><\/p>\n<pre><code>SeehearmeWebapp.Views.Videos ||= {}\n\nclass SeehearmeWebapp.Views.Videos.ShowView extends Backbone.View\n  template: JST[\"backbone\/templates\/videos\/show\"]\n\n  render: -&gt;\n    $(@el).html(@template(@model.toJSON() ))\n    return this\n<\/code><\/pre>\n<p><code>show_view.js.coffee<\/code><\/p>\n<h1><code>THIS IS A VIDEO<\/code><\/h1>\n<pre>\n\nBack\n<\/pre>\n<p><code>Thanks!<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>My app is a rails 3 app using backbone.js and jw player for playing a playlist of videos. The index_view for the videos has all the videos loaded into a playlist for jw player. My problem comes when I navigate away from the index page, let&#8217;s say to the show_view for an individual video. When [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6069","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6069","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6069"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6069\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}