Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone-rails Not Loading Post when Accessed Directly through URL
    primarykey
    data
    text
    <p>So I'm new to Backbone and I developed a Rails App with a JSON API that I've hooked Backbone in to.</p> <p>When I use the site directly and follow the links, every thing works great.</p> <p>As in I click a link that directs to /#/1 and it directs me to the show page where the post follow the ID of one.</p> <p>However, if I refresh the page it no longer displays that information, and throws an error: </p> <blockquote> <p>"Uncaught TypeError: Cannot call method 'toJSON' of undefined "</p> </blockquote> <p>When I go to the console and access my views <code>BackboneBlog.router.show(1)</code> it does work correctly. It is only when I go to <code>http://localhost:3000/#/1</code> that I get an issue. However, when I click on the link in my index page, that links directly to <code>http://localhost:3000/#/1</code></p> <p>The even more hilarious thing is <code>BackboneBlog.router.posts</code> is correct. However, if in my code I type that exactly,but add <code>BackboneBlog.router.posts.get(1)</code>, it returns undefined. However, if I run that exact code in the console with the same URL, it works. </p> <p>Here is my code: </p> <blockquote> <p>posts_router.js.coffee--</p> </blockquote> <pre><code>class BackboneBlog.Routers.PostsRouter extends Backbone.Router initialize: -&gt; @posts = new BackboneBlog.Collections.PostsCollection() @posts.fetch() routes: "": "index" "new" : "newPost" "index" : "index" ":id/edit" : "edit" ":id" : "show" ".*" : "index" newPost: -&gt; @view = new BackboneBlog.Views.Posts.NewView(collection: @posts) $("#posts").html(@view.render().el) index: -&gt; @view = new BackboneBlog.Views.Posts.IndexView(posts: @posts) $("#posts").html("") $("#posts").html(@view.render().el) show: (id) -&gt; console.log(@posts) #puts PostsCollection, with posts in it console.log(id) #puts 1 @post = @posts.get(id) console.log(@post) #puts undefined @view = new BackboneBlog.Views.Posts.ShowView(model: @post) $("#posts").html(@view.render().el) edit: (id) -&gt; post = @posts.get(id) @view = new BackboneBlog.Views.Posts.EditView(model: post) $("#posts").html(@view.render().el) </code></pre> <blockquote> <p>show_view.js.coffee -- </p> </blockquote> <pre><code>BackboneBlog.Views.Posts ||= {} class BackboneBlog.Views.Posts.ShowView extends Backbone.View template: JST["backbone/templates/posts/show"] render: -&gt; $(@el).html(@template(@model.toJSON() )) return this </code></pre> <blockquote> <p>index_view.coffee --</p> </blockquote> <pre><code>BackboneBlog.Views.Posts ||= {} class BackboneBlog.Views.Posts.IndexView extends Backbone.View template: JST["backbone/templates/posts/index"] initialize: () -&gt; @options.posts.bind('reset', @addAll) addAll: () =&gt; $("tbody").html("") @options.posts.each(@addOne) addOne: (post) =&gt; view = new BackboneBlog.Views.Posts.PostView({model : post}) @$("table").append(view.render().el) render: =&gt; $(@el).html(@template(posts: @options.posts.toJSON() )) @addAll() return this </code></pre> <blockquote> <p>backbone_blog.js.coffee -- </p> </blockquote> <pre><code>window.BackboneBlog = Models: {} Collections: {} Routers: {} Views: {} init: -&gt; @router = new BackboneBlog.Routers.PostsRouter() Backbone.history.start() </code></pre> <p>Thanks All! &lt;3 </p> <p>Edit:<br> tl;dr: If I directly access <code>http://localhost:3000/#/1</code> I get errors, however, if I link there, it works perfectly fine. If I type it in the console, it works fine. It is only when I navigate directly to the URL that it does not work. Even still, in the show method, every thing works until I directly get the post</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload