learning_info.js 1.74 KB
Newer Older
1 2
// Backbone Application View: Course Learning Information

3
define([
4 5 6 7 8 9
    'jquery',
    'underscore',
    'backbone',
    'gettext',
    'js/utils/templates'
],
10
function($, _, Backbone, gettext, TemplateUtils) {
11 12 13 14
    'use strict';
    var LearningInfoView = Backbone.View.extend({

        events: {
15
            'click .delete-course-learning-info': 'removeLearningInfo'
16 17 18 19
        },

        initialize: function() {
            // Set up the initial state of the attributes set for this model instance
20
            _.bindAll(this, 'render');
21 22 23 24 25 26 27 28 29 30 31
            this.template = this.loadTemplate('course-settings-learning-fields');
            this.listenTo(this.model, 'change:learning_info', this.render);
        },

        loadTemplate: function(name) {
            // Retrieve the corresponding template for this model
            return TemplateUtils.loadTemplate(name);
        },

        render: function() {
             // rendering for this model
32
            $('li.course-settings-learning-fields').empty();
33 34
            var self = this;
            var learning_information = this.model.get('learning_info');
35 36
            $.each(learning_information, function(index, info) {
                $(self.el).append(self.template({index: index, info: info, info_count: learning_information.length}));
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
            });
        },

        removeLearningInfo: function(event) {
            /*
            * Remove course learning fields.
            * */
            event.preventDefault();
            var index = event.currentTarget.getAttribute('data-index'),
                existing_info = _.clone(this.model.get('learning_info'));
            existing_info.splice(index, 1);
            this.model.set('learning_info', existing_info);
        }

    });
    return LearningInfoView;
});