Reputation: 526
I have build a website where the contents are being transferred dynamically, with nodejs
and expressjs
with jade
template engine, on the back end providing the REST API and backbone.js
managed with require.js
, using the underscore
templates on the front end.
Contents are like Questions and Answers OR a review post about a product. Currently i am sending all these content as JSON
and the Front end receiving view
of backbone.js
has the necessary javascript and the correct underscore templates to render the final html on the page.
This is fine when these content are to be seen as a feed, which get dynamically loaded on scroll.
But i want to have an separate page for each of these posts, like a separate page for a Question and related answer and a separate page for a review. I want these pages to be efficiently indexed by Search Engines, so i want to transfer them as the finally generated HTML
, rather than a require.js main entry with contents getting populated after a series of ajax call.
One way to solve this is by using the express.js views to build the html on the server side and then inject all the necessary javascript in it and send it. But i don't want to repeat my logic.
I there any way that i can re-use the code logic written in backbone.js for template rendering with supplied variables and injection of js, all this on the server side and then simple send the generated final html to the client, so that if they see the source code of the page, they can see the full content.
Thanks
Upvotes: 0
Views: 1142
Reputation: 130
If you are rendering on the server-side, one way to proceed is to bind the view to an element:
var View = Backbone.View.extend({
el: "#yourSuperId"
});
new View();
Using the el
property, you can automatically attach the view to the existing element. You don't need to render it.
Upvotes: 3