|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var Backbone = require('../vendor/index').Backbone; |
| 4 | +var _ = require('../vendor/index')._; |
| 5 | +var $ = require('../vendor/index').$; |
| 6 | + |
| 7 | +var fs = require('fs'); //will be replaced by brfs in the browser |
| 8 | + |
| 9 | +// readFileSync will be evaluated statically so errors can't be caught |
| 10 | +var template = fs.readFileSync(__dirname + '/learning-resources.html', 'utf8'); |
| 11 | + |
| 12 | +module.exports = Backbone.View.extend({ |
| 13 | + |
| 14 | + className: 'learning-resource', |
| 15 | + |
| 16 | + template: _.template(template), |
| 17 | + |
| 18 | + events: { |
| 19 | + 'click .b-delete': 'destroy', |
| 20 | + 'click .b-edit': 'edit', |
| 21 | + 'click .b-cancel': 'cancel', |
| 22 | + 'click .b-update': 'save' |
| 23 | + }, |
| 24 | + |
| 25 | + initialize: function(){ |
| 26 | + this.listenTo(this.model, 'destroy', this.remove); |
| 27 | + this.listenTo(this.model, 'change', this.render); |
| 28 | + }, |
| 29 | + |
| 30 | + edit: function(){ |
| 31 | + var resource = this.model.get('resourceType'); |
| 32 | + this.$el.addClass('editing'); //displays input fields |
| 33 | + $('#form-area').addClass('sty-form'); //add bg color of form |
| 34 | + }, |
| 35 | + |
| 36 | + cancel: function(){ |
| 37 | + this.$('#title').val(this.model.get('title')); |
| 38 | + this.$('#desc').val(this.model.get('description')); |
| 39 | + this.$('#auth').val(this.model.get('authors')); |
| 40 | + this.$('select option[value="'+this.model.get('resourceType')+'"]') |
| 41 | + .attr('selected','selected'); |
| 42 | + $('#form-area').removeClass('sty-form'); //remove bg color of form |
| 43 | + $('#msg').empty() |
| 44 | + .addClass('alert-warning') |
| 45 | + .html('Changes cancelled') |
| 46 | + .fadeIn().delay(2000).fadeOut('slow') |
| 47 | + .queue(function(remove){ |
| 48 | + $('#msg').removeClass('alert-warning'); |
| 49 | + remove(); |
| 50 | + }); |
| 51 | + this.$el.removeClass('editing'); |
| 52 | + }, |
| 53 | + |
| 54 | + save: function(){ |
| 55 | + var view = this; |
| 56 | + var auth = []; |
| 57 | + if (this.$('#auth').val() === '') auth = null; |
| 58 | + else $.each(this.$('#auth').val().split(','), function(){ |
| 59 | + auth.push($.trim(this)); |
| 60 | + }); |
| 61 | + var attributes = { |
| 62 | + title: this.$('#title').val().trim(), |
| 63 | + resourceType: $('#resourceType option:selected').val(), |
| 64 | + description: this.$('#desc').val().trim(), |
| 65 | + authors: auth |
| 66 | + }; |
| 67 | + var options = { |
| 68 | + success: function(){ |
| 69 | + $('#form-area').removeClass('sty-form'); |
| 70 | + view.$el.removeClass('editing'); |
| 71 | + $('#msg').empty() |
| 72 | + .addClass('alert-success') |
| 73 | + .html('Sucessfully updated') |
| 74 | + .fadeIn().delay(2000).fadeOut('slow') |
| 75 | + .queue(function(remove){ |
| 76 | + $('#msg').removeClass('alert-success'); |
| 77 | + remove(); |
| 78 | + }); |
| 79 | + }, |
| 80 | + error: function(model, error){ |
| 81 | + //server response errors if no validations specified |
| 82 | + } |
| 83 | + }; |
| 84 | + this.model.save(attributes, options); |
| 85 | + if (this.model.validationError) { |
| 86 | + $('#msg').empty() |
| 87 | + .addClass('alert-danger') |
| 88 | + .html(this.model.validationError) |
| 89 | + .fadeIn().delay(2000).fadeOut('slow') |
| 90 | + .queue(function(remove){ |
| 91 | + $('#msg').removeClass('alert-danger'); |
| 92 | + remove(); |
| 93 | + }); |
| 94 | + } |
| 95 | + }, |
| 96 | + |
| 97 | + render: function(){ |
| 98 | + var context = this.model.toJSON(); |
| 99 | + var auth = this.model.get('authors').toString().split(',').join(', '); |
| 100 | + this.$el.html(this.template(context)); |
| 101 | + this.input = this.$('.editing'); |
| 102 | + this.$('select option[value="'+this.model.get('resourceType')+'"]') |
| 103 | + .attr('selected','selected'); |
| 104 | + this.$('#auth').val(auth); |
| 105 | + this.$('#author-dsp').text(auth); |
| 106 | + return this; |
| 107 | + }, |
| 108 | + |
| 109 | + destroy: function(){ |
| 110 | + this.model.destroy(); |
| 111 | + this.remove(); |
| 112 | + } |
| 113 | +}); |
0 commit comments