Commit 80f5d421 by Will Daly

Inline underscore templates; add pre render step

parent cb1058ac
...@@ -25,8 +25,6 @@ from django.utils.decorators import method_decorator ...@@ -25,8 +25,6 @@ from django.utils.decorators import method_decorator
from django.utils.translation import ugettext as _, ugettext_lazy from django.utils.translation import ugettext as _, ugettext_lazy
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from staticfiles.storage import staticfiles_storage
from openedx.core.djangoapps.user_api.api import profile as profile_api from openedx.core.djangoapps.user_api.api import profile as profile_api
from course_modes.models import CourseMode from course_modes.models import CourseMode
...@@ -270,31 +268,31 @@ class PayAndVerifyView(View): ...@@ -270,31 +268,31 @@ class PayAndVerifyView(View):
STEP_INFO = { STEP_INFO = {
INTRO_STEP: Step( INTRO_STEP: Step(
title=ugettext_lazy("Intro"), title=ugettext_lazy("Intro"),
template_name="verify_student/intro_step.underscore" template_name="intro_step"
), ),
MAKE_PAYMENT_STEP: Step( MAKE_PAYMENT_STEP: Step(
title=ugettext_lazy("Make Payment"), title=ugettext_lazy("Make Payment"),
template_name="verify_student/make_payment_step.underscore" template_name="make_payment_step"
), ),
PAYMENT_CONFIRMATION_STEP: Step( PAYMENT_CONFIRMATION_STEP: Step(
title=ugettext_lazy("Payment Confirmation"), title=ugettext_lazy("Payment Confirmation"),
template_name="verify_student/payment_confirmation_step.underscore" template_name="payment_confirmation_step"
), ),
FACE_PHOTO_STEP: Step( FACE_PHOTO_STEP: Step(
title=ugettext_lazy("Take Face Photo"), title=ugettext_lazy("Take Face Photo"),
template_name="verify_student/face_photo_step.underscore" template_name="face_photo_step"
), ),
ID_PHOTO_STEP: Step( ID_PHOTO_STEP: Step(
title=ugettext_lazy("ID Photo"), title=ugettext_lazy("ID Photo"),
template_name="verify_student/id_photo_step.underscore" template_name="id_photo_step"
), ),
REVIEW_PHOTOS_STEP: Step( REVIEW_PHOTOS_STEP: Step(
title=ugettext_lazy("Review Photos"), title=ugettext_lazy("Review Photos"),
template_name="verify_student/review_photos_step.underscore" template_name="review_photos_step"
), ),
ENROLLMENT_CONFIRMATION_STEP: Step( ENROLLMENT_CONFIRMATION_STEP: Step(
title=ugettext_lazy("Enrollment Confirmation"), title=ugettext_lazy("Enrollment Confirmation"),
template_name="verify_student/enrollment_confirmation_step.underscore" template_name="enrollment_confirmation_step"
), ),
} }
...@@ -607,34 +605,12 @@ class PayAndVerifyView(View): ...@@ -607,34 +605,12 @@ class PayAndVerifyView(View):
{ {
'name': step, 'name': step,
'title': unicode(self.STEP_INFO[step].title), 'title': unicode(self.STEP_INFO[step].title),
'templateUrl': self._template_url(self.STEP_INFO[step].template_name) 'templateName': self.STEP_INFO[step].template_name
} }
for step in display_steps for step in display_steps
if step not in remove_steps if step not in remove_steps
] ]
def _template_url(self, template_name):
"""Determine the path to a template.
This uses staticfiles, so the path will include MD5
hashes when used in production. This is really important,
because otherwise the JavaScript won't be able to find
the templates!
Arguments:
template_name (str): The name of the template, relative
to the "static/templates" directory.
Returns:
string
"""
template_path = u"templates/{name}".format(name=template_name)
return (
staticfiles_storage.url(template_path)
if template_name is not None else ""
)
def _requirements(self, display_steps): def _requirements(self, display_steps):
"""Determine which requirements to show the user. """Determine which requirements to show the user.
......
...@@ -96,7 +96,7 @@ var edx = edx || {}; ...@@ -96,7 +96,7 @@ var edx = edx || {};
subviewConfig = { subviewConfig = {
errorModel: this.errorModel, errorModel: this.errorModel,
templateUrl: this.displaySteps[i].templateUrl, templateName: this.displaySteps[i].templateName,
nextStepNum: (i + 2), // Next index, starting from 1 nextStepNum: (i + 2), // Next index, starting from 1
nextStepTitle: nextStepTitle, nextStepTitle: nextStepTitle,
stepData: stepData stepData: stepData
......
/** /**
* Base view for defining steps in the payment/verification flow. * Base view for defining steps in the payment/verification flow.
* *
* Each step view lazy-loads its underscore template.
* This reduces the size of the initial page, since we don't
* need to include the DOM structure for each step
* in the initial load.
*
* Step subclasses are responsible for defining a template * Step subclasses are responsible for defining a template
* and installing custom event handlers (including buttons * and installing custom event handlers (including buttons
* to move to the next step). * to move to the next step).
* *
* The superclass is responsible for downloading the underscore
* template and rendering it, using context received from
* the server (in data attributes on the initial page load).
*
*/ */
var edx = edx || {}; var edx = edx || {};
...@@ -35,18 +26,27 @@ ...@@ -35,18 +26,27 @@
}, },
render: function() { render: function() {
if ( !this.renderedHtml && this.templateUrl) { var templateHtml = $( "#" + this.templateName + "-tpl" ).html(),
$.ajax({ templateContext = {
url: this.templateUrl, nextStepNum: this.nextStepNum,
type: 'GET', nextStepTitle: this.nextStepTitle
context: this, };
success: this.handleResponse,
error: this.handleError // Include step-specific information from the server
}); // (passed in from data- attributes to the parent view)
} else { _.extend( templateContext, this.stepData );
$( this.el ).html( this.renderedHtml );
this.postRender(); // Allow subclasses to add additional information
} // to the template context, perhaps asynchronously.
this.updateContext( templateContext ).done(
function( templateContext ) {
// Render the template into the DOM
$( this.el ).html( _.template( templateHtml, templateContext ) );
// Allow subclasses to install custom event handlers
this.postRender();
}
).fail( _.bind( this.handleError, this ) );
}, },
handleResponse: function( data ) { handleResponse: function( data ) {
...@@ -66,12 +66,28 @@ ...@@ -66,12 +66,28 @@
handleError: function() { handleError: function() {
this.errorModel.set({ this.errorModel.set({
errorTitle: gettext("Error"), errorTitle: gettext( "Error" ),
errorMsg: gettext("An unexpected error occurred. Please reload the page to try again."), errorMsg: gettext( "An unexpected error occurred. Please reload the page to try again." ),
shown: true shown: true
}); });
}, },
/**
* Subclasses can override this to add information to
* the template context. This returns an asynchronous
* Promise, so the subclass can fill in the template
* after completing an AJAX request.
* The default implementation is a no-op.
*/
updateContext: function( templateContext ) {
var view = this;
return $.Deferred(
function( defer ) {
defer.resolveWith( view, [ templateContext ]);
}
).promise();
},
postRender: function() { postRender: function() {
// Sub-classes can override this method // Sub-classes can override this method
// to install custom event handlers. // to install custom event handlers.
......
...@@ -9,7 +9,13 @@ from verify_student.views import PayAndVerifyView ...@@ -9,7 +9,13 @@ from verify_student.views import PayAndVerifyView
<%block name="bodyclass">register verification-process step-requirements</%block> <%block name="bodyclass">register verification-process step-requirements</%block>
<%block name="pagetitle">${messages.page_title}</%block> <%block name="pagetitle">${messages.page_title}</%block>
<%block name="header_extras"> <%block name="header_extras">
% for template_name in ["progress", "requirements", "webcam_photo", "error"]: <%
template_names = (
["progress", "requirements", "webcam_photo", "error"] +
[step['templateName'] for step in display_steps]
)
%>
% for template_name in template_names:
<script type="text/template" id="${template_name}-tpl"> <script type="text/template" id="${template_name}-tpl">
<%static:include path="verify_student/${template_name}.underscore" /> <%static:include path="verify_student/${template_name}.underscore" />
</script> </script>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment