This is done in JS because that's how the rest of this page handles backend interaction in an ajaxy way:  if you look further down at the "Register" button, that's how it works also, and I wanted to be consistent with how the page is previously implemented.

I like the ajax style is good for handling the error cases since it can displaying error messages to the user without re-rendering a whole new template.  As it stands now, the only thing handled by the JS is the redirection.  The backend does the adding to the cart.  I don't see that as too different from the backend adding to the cart and then redirecting.  The only actual difference is whether the backend returns a 200 or a 302.  And if the backend returns a 302 right now, it's the XHR that gets retried at the different URL.  The whole page doesn't get redirected.

On Sep 25, 2013, at 8:45 AM, Diana Huang <notifications@github.com> wrote:

> In lms/templates/courseware/course_about.html:
> 
> > +            $("#register_error")
> > +                .html(jqXHR.responseText ? jqXHR.responseText : "${_('An error occurred. Please try again later.')}")
> > +                .css("display", "block");
> > +        }
> > +        else if (jqXHR.status == 403) {
> > +            location.href = "${reg_then_add_to_cart_link}";
> > +        }
> > +    };
> > +    $("#add_to_cart_post").click(function(event){
> > +        $.ajax({
> > +            url: "${reverse('add_course_to_cart', args=[course.id])}",
> > +            type: "POST",
> > +            /* Rant: HAD TO USE COMPLETE B/C PROMISE.DONE FOR SOME REASON DOES NOT WORK ON THIS PAGE. */
> > +            complete: add_course_complete_handler
> > +        })
> > +        event.preventDefault();
> I'm confused by why we're doing this in Javascript. Why aren't we creating a backend view to add the course to the cart and then forward them to the right next destination?
> 
> —
> Reply to this email directly or view it on GitHub.
>