0

Hi I am new to backbone and I understand that you essentially route javascript to HTML pages, but I cant seem to route any javascript to the application.html.erb since it doesnt have a specific URL.

I can solve this problem, by removing the HTML I want to work on and move it to the index page, but this really isnt what I want.

My "solution"

views/posts/post_index.js.coffee

class Poster.Views.PostsIndex extends Backbone.View

  template: JST['posts/index']

  events:
    'click #new_post': 'createPost'

  initialize: ->
    @collection.on('reset', @render, this)
    @collection.on('add', @appendEntry, this)

  render: ->
    $(@el).html(@template())
    @collection.each(@appendEntry)
    this

  createPost: (event) ->
    Backbone.history.navigate("/posts/new", true)

  appendEntry: (post) ->
    view = new Poster.Views.Post(model: post)
    $('#posts_container').append(view.render().el)

index.jst.eco

<div id="menu_bar" class="jumbotron menu_bar row">
    <div class="col-sm-2">
        <button id="new_post" type="button" class="btn btn-default">Post</button>
    </div>
    <div class="col-offset-8 col-sm-2">
        List
    </div>
</div>
3
  • What is the question? Normally the "HTML you want to work on" would be built from the view's template and then something in your router or page initialization would instantiate and render the view. Commented Aug 2, 2013 at 6:20
  • Yes thats correct, but application.html.erb is a file in rails that wraps around all other HTML and it does not have a URL you can access Commented Aug 2, 2013 at 17:56
  • I don't see what application.html.erb has to do with anything. Your server is going to return an HTML page to bootstrap the Backbone application, then its up to your router and JavaScript initialization to get Backbone going and then its all up to Backbone and your templates. Commented Aug 2, 2013 at 19:00

1 Answer 1

1

With front end frameworks your best bet is to remove anything you need js access to out of the application.html file. If you really wanted to keep that stuff in the application.html file, you could give each element you want access to an id so that you can easily find it with javascript. Other than that your best bet would be to move everything into the index page like you said.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.