Skip to content

Commit

Permalink
Add a form of static:require_module that assumes that the factory is …
Browse files Browse the repository at this point in the history
…self-initializing
  • Loading branch information
cpennington committed Jan 18, 2018
1 parent 7858066 commit bacd433
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
3 changes: 3 additions & 0 deletions cms/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,9 @@

# Entitlements, used in openedx tests
'entitlements',

# Asset management for mako templates
'pipeline_mako',
]


Expand Down
1 change: 1 addition & 0 deletions cms/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
});
});
</script>
<%block name='requirejs_page'></%block>
<%include file="widgets/segment-io-footer.html" />
<div class="modal-cover"></div>
</body>
Expand Down
34 changes: 32 additions & 2 deletions common/djangoapps/pipeline_mako/templates/static_content.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@
${HTML(render_bundle(page))}
</%def>

<%def name="webpack(entry)">
<%def name="webpack(entry, extension=None, config='DEFAULT', attrs='')">
<%doc>
Loads Javascript onto your page from a Webpack-generated bundle.
Uses the Django template engine because our webpack loader only provides template tags for Jinja and Django.
</%doc>
<%
body = capture(caller.body)
%>
${HTML(render_bundle(entry))}
${HTML(render_bundle(entry, extension=None, config='DEFAULT', attrs=attrs))}
% if body:
<script type="text/javascript">
${body | n, decode.utf8}
Expand Down Expand Up @@ -154,6 +154,36 @@
</script>
</%def>

<%def name="require_page(page_name, class_name)">
<%doc>
Loads Javascript onto your page synchronously.
Uses RequireJS in development and a plain script tag in production.
The body of the tag should be a comma-separated list of arguments
to be passed to the page factory specified by the class_name argument.
</%doc>
<%
body = capture(caller.body)
%>
<script type="text/javascript">
if (typeof pageFactoryArguments == "undefined") {
var pageFactoryArguments = {};
}
% if body:
pageFactoryArguments['${class_name | n, js_escaped_string}'] = [${ body | n, decode.utf8 }];
% else:
pageFactoryArguments['${class_name | n, js_escaped_string}'] = [];
% endif
</script>
% if not settings.REQUIRE_DEBUG:
<script type="text/javascript" src="${staticfiles_storage.url(page_name + '.js') + '?raw'}"></script>
% endif
<script type="text/javascript">
(function (require) {
require(['${page_name | n, js_escaped_string}']);
}).call(this, require || RequireJS.require);
</script>
</%def>

<%def name="require_module(module_name, class_name)">
<%doc>
Loads Javascript onto your page synchronously.
Expand Down
27 changes: 27 additions & 0 deletions common/static/common/js/utils/page_factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
define([], function() {
'use strict';

return function invokePageFactory(name, factory) {
var args;

if (typeof window.pageFactoryArguments === 'undefined') {
throw Error(
'window.pageFactoryArguments must be initialized before calling invokePageFactory(' +
name +
'). Use the <%static:require_page> template tag.'
);
}
args = window.pageFactoryArguments[name];

if (typeof args === 'undefined') {
throw Error(
'window.pageFactoryArguments["' +
name +
'"] must be initialized before calling invokePageFactory(' +
name +
'). Use the <%static:require_page> template tag.'
);
}
factory.apply(null, window.pageFactoryArguments[name]);
};
});
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
def no_webpack_loader(monkeypatch):
monkeypatch.setattr(
"webpack_loader.templatetags.webpack_loader.render_bundle",
lambda x: ''
lambda entry, extension=None, config='DEFAULT', attrs='': ''
)

0 comments on commit bacd433

Please sign in to comment.