Add a form of static:require_module that assumes that the factory is self-initializing

This commit is contained in:
Calen Pennington
2017-12-21 15:29:53 -05:00
parent 7858066776
commit bacd4334ed
5 changed files with 64 additions and 3 deletions

View File

@@ -1126,6 +1126,9 @@ INSTALLED_APPS = [
# Entitlements, used in openedx tests
'entitlements',
# Asset management for mako templates
'pipeline_mako',
]

View File

@@ -143,6 +143,7 @@ from openedx.core.djangolib.markup import HTML
});
});
</script>
<%block name='requirejs_page'></%block>
<%include file="widgets/segment-io-footer.html" />
<div class="modal-cover"></div>
</body>

View File

@@ -111,7 +111,7 @@ source, template_path = Loader(engine).load_template_source(path)
${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.
@@ -119,7 +119,7 @@ source, template_path = Loader(engine).load_template_source(path)
<%
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}
@@ -154,6 +154,36 @@ source, template_path = Loader(engine).load_template_source(path)
</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.

View File

@@ -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]);
};
});

View File

@@ -14,5 +14,5 @@ from cms.conftest import _django_clear_site_cache, pytest_configure # pylint: d
def no_webpack_loader(monkeypatch):
monkeypatch.setattr(
"webpack_loader.templatetags.webpack_loader.render_bundle",
lambda x: ''
lambda entry, extension=None, config='DEFAULT', attrs='': ''
)