Supposed I have two different Objects I am trying to load in an eventual BackBone view.
My Rails Controller
def index
@visuals = Visual.all
@words = Word.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: [@visuals, @words] }
end
end
On place I can add it is here :
My Rails View:
#visuals
:javascript
$(function() {
window.router = new AiProject.Routers.VisualsRouter({test_visuals: #{ @visuals.to_json.html_safe } });
// AiProject.words = new Words(#{@words.to_json});
// ^^ Would this be the best place to insantiate my controller code's instance variable?
Backbone.history.start();
});
But other people say to put it in your template. Which is strange because I think my BackBone’s framework puts it in the router:
Coffee BB Router
show: (id) ->
visual = @visuals.get(id)
@view = new AiProject.Views.Visuals.ShowView(model: visual)
$("#visuals").html(@view.render().el)
So if that was the case, maybe I should try and insantiate @words there?
edit – this was not talking about bootstrapping.
You need to be calling the
indexaction somewhere, probably directly viajQuery.ajaxas it doesn’t correspond directly to a single backbone model or collection.In the success handler of your ajax call, you can assign the returned data to whatever you want to assign it to in backbone.
I would probably have models for single visuals and words, then collections of each as well (in raw javascript as i’m out of practice with coffeescript at the moment):
at this point, you have your
visualsandwordscollections, and can do whatever you need to do with them.