Skip to content

Commit 96b0c53

Browse files
committed
Merge pull request #26 from saponifi3d/fix_double_initialization
Updated the view helper to only create an instance of the view
2 parents 4c99355 + 473c7f7 commit 96b0c53

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rendr-handlebars",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Glue handlebars templates into a Rendr app.",
55
"main": "index.js",
66
"scripts": {
@@ -15,7 +15,7 @@
1515
"handlebars": "1.3.0"
1616
},
1717
"peerDependencies": {
18-
"rendr": ">=0.5.0"
18+
"rendr": ">=0.5.1"
1919
},
2020
"keywords": [
2121
"rendr",

shared/helpers.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,49 @@ module.exports = function(Handlebars, getTemplate) {
1717
}
1818
viewOptions = options.hash || {};
1919

20-
// Pass through a reference to the app.
2120
var app = getProperty('_app', this, options);
21+
// Pass through a reference to the app.
2222
if (app) {
2323
viewOptions.app = app;
2424
viewName = app.modelUtils.underscorize(viewName);
2525
} else{
2626
throw new Error("An App instance is required when rendering a view, it could not be extracted from the options.")
2727
}
2828

29-
// Pass through a reference to the parent view.
30-
var parentView = getProperty('_view', this, options);
31-
if (parentView) {
32-
viewOptions.parentView = parentView;
29+
if (isServer) {
30+
// Pass through a reference to the parent view.
31+
var parentView = getProperty('_view', this, options);
32+
if (parentView) {
33+
viewOptions.parentView = parentView;
34+
}
35+
36+
// get the Backbone.View based on viewName
37+
ViewClass = BaseView.getView(viewName, app.options.entryPath);
38+
view = new ViewClass(viewOptions);
39+
40+
// create the outerHTML using className, tagName
41+
html = view.getHtml();
42+
return new Handlebars.SafeString(html);
3343
}
3444

35-
// get the Backbone.View based on viewName
36-
ViewClass = BaseView.getView(viewName, app.options.entryPath);
37-
view = new ViewClass(viewOptions);
45+
// only create an element to pass attributes to a single point for view instantiation for client-side
3846

39-
// create the outerHTML using className, tagName
40-
html = view.getHtml();
41-
return new Handlebars.SafeString(html);
47+
// Builds a fetch_summary attribute
48+
viewOptions = BaseView.parseModelAndCollection(app.modelUtils, viewOptions);
49+
var fetchSummary = BaseView.extractFetchSummary(app.modelUtils, viewOptions);
50+
fetchSummary = JSON.stringify(fetchSummary)
51+
52+
viewOptions['fetch_summary'] = fetchSummary
53+
viewOptions = _.omit(viewOptions, ['model', 'collection', 'app'])
54+
55+
// create a list of data attributes
56+
var attrString = _.inject(viewOptions, function(memo, value, key) {
57+
return memo += " data-" + key + "=\"" + _.escape(value) + "\"";
58+
}, '');
59+
60+
return new Handlebars.SafeString(
61+
'<div data-render="true" ' + attrString +' data-view="'+ viewName +'"></div>'
62+
)
4263
},
4364

4465
partial: function(templateName, options) {

0 commit comments

Comments
 (0)