RegisterModel.js 1.8 KB
Newer Older
1 2
var edx = edx || {};

3
(function($, Backbone) {
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
    'use strict';

    edx.student = edx.student || {};
    edx.student.account = edx.student.account || {};

    edx.student.account.RegisterModel = Backbone.Model.extend({

        defaults: {
            email: '',
            name: '',
            username: '',
            password: '',
            level_of_education: '',
            gender: '',
            year_of_birth: '',
            mailing_address: '',
            goals: '',
        },

23 24
        ajaxType: '',

25 26
        urlRoot: '',

27 28 29
        initialize: function( attributes, options ) {
            this.ajaxType = options.method;
            this.urlRoot = options.url;
30 31 32
        },

        sync: function(method, model) {
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
            var headers = { 'X-CSRFToken': $.cookie('csrftoken') },
                data = {},
                analytics,
                courseId = $.url( '?course_id' );

            // If there is a course ID in the query string param,
            // send that to the server as well so it can be included
            // in analytics events.
            if ( courseId ) {
                analytics = JSON.stringify({
                    enroll_course_id: decodeURIComponent( courseId )
                });
            }

            // Include all form fields and analytics info in the data sent to the server
            $.extend( data, model.attributes, { analytics: analytics });
49 50 51

            $.ajax({
                url: model.urlRoot,
52
                type: model.ajaxType,
53
                data: data,
54 55 56 57 58 59 60
                headers: headers,
                success: function() {
                    model.trigger('sync');
                },
                error: function( error ) {
                    model.trigger('error', error);
                }
61 62 63
            });
        }
    });
64
})(jQuery, Backbone);