From a4ba75d1ed226fbc84005cabe354093926ae467d Mon Sep 17 00:00:00 2001 From: neilchokriwala Date: Mon, 26 Jan 2015 23:28:06 -0800 Subject: [PATCH 1/6] added my initial submission for iss44, does not have much functionality --- client/spa/js/student/student.controller.js | 69 +++++++++++++++++++++ client/spa/js/student/student.html | 3 + client/spa/js/student/student.model.js | 21 +++++++ client/spa/js/student/student.view.js | 38 ++++++++++++ common/models/course.js | 3 + common/models/course.json | 28 +++++++++ common/models/student.js | 6 ++ common/models/student.json | 24 +++++++ server/model-config.json | 4 ++ 9 files changed, 196 insertions(+) create mode 100644 client/spa/js/student/student.controller.js create mode 100644 client/spa/js/student/student.html create mode 100644 client/spa/js/student/student.model.js create mode 100644 client/spa/js/student/student.view.js create mode 100644 common/models/course.js create mode 100644 common/models/course.json create mode 100644 common/models/student.js create mode 100644 common/models/student.json diff --git a/client/spa/js/student/student.controller.js b/client/spa/js/student/student.controller.js new file mode 100644 index 0000000..9e198d8 --- /dev/null +++ b/client/spa/js/student/student.controller.js @@ -0,0 +1,69 @@ +'use strict'; + +var Backbone = require('../vendor/index').Backbone; +var $ = require('../vendor/index').$; +var Model = require('./student.model'); +var View = require('./student.view'); + +module.exports = Backbone.Controller.extend({ + + routes: { + 'students/:id': 'showStudent' + }, + + initialize: function(){ + this.options.container = this.options.container || 'body'; + this.model = new Model(); + this.view = new View({model: this.model}); + }, + + showStudent: function(studentId, cb){ + this.fetchModel(studentId, function(err){ + var view; + + if (err){ + view = this.renderError(); + } else { + view = this.renderView(); + } + + if (cb){ + cb(err, view); + } + + }.bind(this)); + }, + + fetchModel: function(studentId, cb){ + this.model.set({id: studentId}); + + this.model.fetch({ + success: function(model, response, options){ + //console.log(model); + cb(null, model); + }, + error: function(model, response, options){ + //console.error(response); + cb(response, model); + } + }); + }, + + renderToContainer: function(view){ + return $(this.options.container).html(view); + }, + + renderView: function(){ + this.renderToContainer(this.view.render().$el); + return this.view; + }, + + renderError: function(){ + return this.renderToContainer( + '

There was a problem rendering this student

'); + } + +}); + + + diff --git a/client/spa/js/student/student.html b/client/spa/js/student/student.html new file mode 100644 index 0000000..bf3d70a --- /dev/null +++ b/client/spa/js/student/student.html @@ -0,0 +1,3 @@ +

<%- name %>

+

<%- student %>

+ diff --git a/client/spa/js/student/student.model.js b/client/spa/js/student/student.model.js new file mode 100644 index 0000000..2765378 --- /dev/null +++ b/client/spa/js/student/student.model.js @@ -0,0 +1,21 @@ +'use strict'; + +var Backbone = require('../vendor/index').Backbone; + +module.exports = Backbone.Model.extend({ + + + urlRoot: '/api/students', + + initialize: function(){ + this.on('change', function(){ + this.trigger('foo', 'bar'); + }); + }, + + validate: function(attrs){ + if (!attrs.name){ + return 'name cannot be empty'; + } + } +}); diff --git a/client/spa/js/student/student.view.js b/client/spa/js/student/student.view.js new file mode 100644 index 0000000..265e2ba --- /dev/null +++ b/client/spa/js/student/student.view.js @@ -0,0 +1,38 @@ +'use strict'; + +var Backbone = require('../vendor/index').Backbone; +var _ = require('../vendor/index')._; +var $ = require('../vendor/index').$; + +var fs = require('fs'); //will be replaced by brfs in the browser + +// readFileSync will be evaluated statically so errors can't be caught +var template = fs.readFileSync(__dirname + '/student.html', 'utf8'); + +module.exports = Backbone.View.extend({ + + className: 'student', + + template: _.template(template), + + events: { + 'click .delete': 'destroy' + }, + + initialize: function(){ + this.listenTo(this.model, 'destroy', this.remove); + }, + + render: function(){ + var context = this.model.toJSON(); + this.$el.html(this.template(context)); + return this; + }, + + destroy: function(){ + this.$e1.html('

There was a problem destroying this student

'); + this.model.destroy(); + } + +}); + diff --git a/common/models/course.js b/common/models/course.js new file mode 100644 index 0000000..bf3fe8b --- /dev/null +++ b/common/models/course.js @@ -0,0 +1,3 @@ +module.exports = function(Course) { + +}; diff --git a/common/models/course.json b/common/models/course.json new file mode 100644 index 0000000..ae2be8a --- /dev/null +++ b/common/models/course.json @@ -0,0 +1,28 @@ +{ + "name": "course", + "base": "PersistedModel", + "idInjection": true, + "properties": { + "title": { + "type": "string", + "required": true + }, + "courseType": { + "type": "string", + "required": true + }, + "description": { + "type": "string", + "required": true + } + }, + "validations": [], + "relations": { + "instructor":{ + "type": "hasOne", + "model": "instructor" + } + }, + "acls": [], + "methods": [] +} diff --git a/common/models/student.js b/common/models/student.js new file mode 100644 index 0000000..5a11fd5 --- /dev/null +++ b/common/models/student.js @@ -0,0 +1,6 @@ +/** + * Created by nchokriw on 1/26/2015. + */ +module.exports = function(Student) { + +}; diff --git a/common/models/student.json b/common/models/student.json new file mode 100644 index 0000000..3f3b1d4 --- /dev/null +++ b/common/models/student.json @@ -0,0 +1,24 @@ +{ + "name": "student", + "base": "PersistedModel", + "idInjection": true, + "properties": { + "firstName": { + "type": "string", + "required": true + }, + "lastName": { + "type": "string", + "required": true + } + }, + "validations": [], + "relations": { + "courses": { + "type": "hasAndBelongsToMany", + "model": "course" + } + }, + "acls": [], + "methods": [] +} diff --git a/server/model-config.json b/server/model-config.json index 6b39153..8cb5091 100644 --- a/server/model-config.json +++ b/server/model-config.json @@ -25,5 +25,9 @@ "Role": { "dataSource": "db", "public": false + }, + "student": { + "dataSource": "db", + "public": true } } From c274111f08ea57642c1d40a7c881707dfeebe26e Mon Sep 17 00:00:00 2001 From: "frances.go" Date: Sat, 14 Feb 2015 02:20:11 -0800 Subject: [PATCH 2/6] Add student model, views and controller spa and tests --- client/spa/js/student/editStudent.html | 10 ++ .../student/spec/student.controller.spec.js | 94 +++++++++++++ .../spa/js/student/spec/student.model.spec.js | 89 ++++++++++++ .../spa/js/student/spec/student.view.spec.js | 128 ++++++++++++++++++ client/spa/js/student/student.html | 11 +- client/spa/js/student/student.model.js | 14 +- client/spa/js/student/student.view.js | 43 +++++- 7 files changed, 381 insertions(+), 8 deletions(-) create mode 100644 client/spa/js/student/editStudent.html create mode 100644 client/spa/js/student/spec/student.controller.spec.js create mode 100644 client/spa/js/student/spec/student.model.spec.js create mode 100644 client/spa/js/student/spec/student.view.spec.js diff --git a/client/spa/js/student/editStudent.html b/client/spa/js/student/editStudent.html new file mode 100644 index 0000000..a0a266f --- /dev/null +++ b/client/spa/js/student/editStudent.html @@ -0,0 +1,10 @@ +
+
+
+
+
+ + +
+
+
diff --git a/client/spa/js/student/spec/student.controller.spec.js b/client/spa/js/student/spec/student.controller.spec.js new file mode 100644 index 0000000..0294529 --- /dev/null +++ b/client/spa/js/student/spec/student.controller.spec.js @@ -0,0 +1,94 @@ +'use strict'; + +/* +global jasmine, describe, it, expect, beforeEach, afterEach, xdescribe, xit, +spyOn +*/ +// Get the code you want to test +var Controller = require('../student.controller'); +var $ = require('jquery'); +var matchers = require('jasmine-jquery-matchers'); +// Test suite +console.log('test student.controller'); +describe('student controller', function(){ + var controller; + + beforeEach(function(){ + controller = new Controller(); + }); + + it('can be created', function(){ + expect(controller).toBeDefined(); + }); + + describe('when it is created', function(){ + + it('has the expected routes', function(){ + expect(controller.routes).toEqual(jasmine.objectContaining({ + 'students/:id': 'showStudent' + })); + }); + + it('without a container option, uses body as the container', function(){ + expect(controller.options.container).toEqual('body'); + }); + + it('with a container option, uses specified container', function(){ + var ctrl = new Controller({container: '.newcontainer'}); + expect(ctrl.options.container).toEqual('.newcontainer'); + }); + + }); + + describe('when calling showstudent', function(){ + + beforeEach(function(){ + jasmine.addMatchers(matchers); + }); + + var success = function(callbacks){ + controller.model.set({'firstName': 'valid firstName', + 'lastName': 'valid lastName'}); + callbacks.success(controller.model); + }; + + var err = function(callbacks){ + callbacks.error('error', controller.model); + }; + + it('with a valid student id, fetches the model', function(){ + spyOn(controller.model, 'fetch').and.callFake(success); + var cb = function(err, view){ + expect(err).toBeNull(); + expect(controller.model.get('firstName')).toEqual('valid firstName'); + expect(controller.model.get('lastName')).toEqual('valid lastName'); + }; + + controller.showStudent(1, cb); + + }); + + it('with a valid student id, renders the view', function(){ + spyOn(controller.model, 'fetch').and.callFake(success); + spyOn(controller.view, 'render').and.callFake(function(){ + controller.view.$el = 'fake render'; + return controller.view; + }); + var cb = function(err, view){ + expect($('body')).toHaveText(''); + expect(view.cid).toEqual(controller.view.cid); + }; + controller.showStudent(1, cb); + }); + + it('with an invalid student id, renders an error message', function(){ + spyOn(controller.model, 'fetch').and.callFake(err); + var cb = function(err, view){ + expect(err).toBeTruthy(); + expect($('body')).toHaveText( + 'There was a problem rendering this student'); + }; + controller.showStudent('whatid', cb); + }); + }); +}); diff --git a/client/spa/js/student/spec/student.model.spec.js b/client/spa/js/student/spec/student.model.spec.js new file mode 100644 index 0000000..7104bb8 --- /dev/null +++ b/client/spa/js/student/spec/student.model.spec.js @@ -0,0 +1,89 @@ +'use strict'; + +/* +global jasmine, describe, it, expect, beforeEach, afterEach, xdescribe, xit, +spyOn +*/ +// Get the code you want to test +var Model = require('../student.model'); + +// Test suite +console.log('test student.model'); +describe('student model ', function(){ + var model; + + describe('when creating a new model ', function(){ + beforeEach(function(){ + model = new Model(); + }); + + it('has the expected routes', function(){ + expect(model.urlRoot).toEqual('/api/students'); + }); + }); + + describe('when updating the model for student with errorSpy ', function(){ + var errorSpy; + + beforeEach(function(){ + errorSpy = jasmine.createSpy('Invalid'); + model = new Model({ + id: 1, + firstName: 'Frances', + lastName: 'Go' + }); + model.on('invalid', errorSpy); + }); + + it('does not save when firstName is empty ', function(){ + model.set('firstName', null); + model.save(); + expect(errorSpy).toHaveBeenCalled(); + expect(errorSpy.calls.mostRecent().args[0]).toBe(model); + expect(errorSpy.calls.mostRecent().args[1][0]).toEqual( + 'firstName cannot be empty'); + }); + + it('does not save when lastName is empty ', function(){ + model.set('lastName', null); + model.save(); + expect(errorSpy).toHaveBeenCalled(); + expect(errorSpy.calls.mostRecent().args[0]).toBe(model); + expect(errorSpy.calls.mostRecent().args[1][0]).toEqual( + 'lastName cannot be empty'); + }); + }); + + describe('when changing the state of the model without errorSpy', function(){ + + beforeEach(function(){ + + model = new Model({ + id: 1, + firstName: 'Mike', + lastName: 'Foster' + }); + + }); + + it('does not save when firstName is empty ', function(){ + model.set('firstName', null); + model.save(); + expect(model.validationError).toEqual(['firstName cannot be empty']); + }); + + it('does not save when lastName is empty ', function(){ + model.set('lastName', null); + model.save(); + expect(model.validationError).toEqual(['lastName cannot be empty']); + }); + + it('does not save when firstName and lastName are empty ', function(){ + model.set({firstName:null, lastName:null}); + model.save(); + expect(model.validationError).toEqual(['firstName cannot be empty', + 'lastName cannot be empty']); + }); + + }); +}); diff --git a/client/spa/js/student/spec/student.view.spec.js b/client/spa/js/student/spec/student.view.spec.js new file mode 100644 index 0000000..85e898c --- /dev/null +++ b/client/spa/js/student/spec/student.view.spec.js @@ -0,0 +1,128 @@ +'use strict'; + +/* +global jasmine, describe, it, expect, beforeEach, afterEach, xdescribe, xit, +spyOn +*/ +// Get the code you want to test +var View = require('../student.view.js'); +var matchers = require('jasmine-jquery-matchers'); +var Backbone = require('../../vendor/index').Backbone; +// Test suite +console.log('test student.view'); +describe('student view ', function(){ + + var model; + var view; + var Model; + + beforeEach(function(){ + + // Add some convenience tests for working with the DOM + jasmine.addMatchers(matchers); + Model = Backbone.Model.extend({}); + + spyOn(Model.prototype, 'save'); + + // Needs to have the fields required by the template + model = new Model({ + firstName: 'Jojo', + lastName: 'Jones' + }); + + view = new View({ + model: model + }); + }); + + describe('when the view is instantiated ', function(){ + + it('creates the correct element', function(){ + + // Element has to be uppercase + expect(view.el.nodeName).toEqual('DIV'); + + }); + + it('sets the correct class', function(){ + expect(view.$el).toHaveClass('student'); + }); + }); + + describe('when the view is rendered ', function(){ + it('returns the view object ', function(){ + expect(view.render()).toEqual(view); + }); + + it('produces the correct HTML ', function(){ + view.render(); + expect(view.$('h1').html()).toEqual('Jojo Jones'); + }); + }); + + describe('when the user clicks on the Edit button ', function(){ + beforeEach(function(){ + // do all spyOn before rendering + spyOn(view, 'save').and.callThrough(); + spyOn(view, 'cancel').and.callThrough(); + // call delegate after spyOn + view.delegateEvents(); + view.render(); + view.$('.s-edit').trigger('click'); + }); + + describe('when the user enters new student information ', function(){ + + describe('when user clicks on the cancel button', function(){ + + beforeEach(function(){ + view.$('.s-cancel').trigger('click'); + }); + + it('cancels the user input', function(){ + expect(view.cancel).toHaveBeenCalled(); + }); + }); + + describe('when user clicks on the save button', function(){ + beforeEach(function(){ + view.$('#firstName').val('changed firstName'); + view.$('#lastName').val('changed lastName'); + + view.$('.s-save').trigger('click'); + }); + + it('updates the model', function(){ + expect(view.save).toHaveBeenCalled(); + expect(Model.prototype.save).toHaveBeenCalled(); + }); + + }); + + }); + + }); // end edit/update test + + describe('when the user clicks on the Delete button ', function(){ + + beforeEach(function(){ + + // Must call through otherwise the actual view function won't be called + spyOn(view, 'destroy').and.callThrough(); + + // Must delegateEvents for the spy on a DOM event to work + view.delegateEvents(); + spyOn(model, 'destroy'); + }); + + it('deletes the model', function(){ + // Must render for the event to be fired + view.render(); + view.$('.s-delete').trigger('click'); + expect(view.destroy).toHaveBeenCalled(); + expect(model.destroy).toHaveBeenCalled(); + }); // end delete model test + + }); // end delete + +}); // end entire suite diff --git a/client/spa/js/student/student.html b/client/spa/js/student/student.html index bf3d70a..4fbb221 100644 --- a/client/spa/js/student/student.html +++ b/client/spa/js/student/student.html @@ -1,3 +1,8 @@ -

<%- name %>

-

<%- student %>

- +
+
+

<%- firstName %> <%- lastName %>

+ + +
+
+
diff --git a/client/spa/js/student/student.model.js b/client/spa/js/student/student.model.js index 2765378..a6798ce 100644 --- a/client/spa/js/student/student.model.js +++ b/client/spa/js/student/student.model.js @@ -3,7 +3,10 @@ var Backbone = require('../vendor/index').Backbone; module.exports = Backbone.Model.extend({ - + defaults: { + firstName: '', + lastName: '' + }, urlRoot: '/api/students', @@ -14,8 +17,13 @@ module.exports = Backbone.Model.extend({ }, validate: function(attrs){ - if (!attrs.name){ - return 'name cannot be empty'; + var errors = []; + if (!attrs.firstName){ + errors.push('firstName cannot be empty'); + } + if (!attrs.lastName){ + errors.push('lastName cannot be empty'); } + return errors; } }); diff --git a/client/spa/js/student/student.view.js b/client/spa/js/student/student.view.js index 265e2ba..0a3b216 100644 --- a/client/spa/js/student/student.view.js +++ b/client/spa/js/student/student.view.js @@ -8,6 +8,7 @@ var fs = require('fs'); //will be replaced by brfs in the browser // readFileSync will be evaluated statically so errors can't be caught var template = fs.readFileSync(__dirname + '/student.html', 'utf8'); +var editTemplate = fs.readFileSync(__dirname + '/editStudent.html', 'utf8'); module.exports = Backbone.View.extend({ @@ -15,12 +16,18 @@ module.exports = Backbone.View.extend({ template: _.template(template), + editTemplate: _.template(editTemplate), + events: { - 'click .delete': 'destroy' + 'click .s-delete': 'destroy', + 'click .s-edit': 'edit', + 'click .s-save': 'save', + 'click .s-cancel': 'cancel' }, initialize: function(){ this.listenTo(this.model, 'destroy', this.remove); + this.listenTo(this.model, 'change', this.render); }, render: function(){ @@ -30,8 +37,40 @@ module.exports = Backbone.View.extend({ }, destroy: function(){ - this.$e1.html('

There was a problem destroying this student

'); this.model.destroy(); + }, + + edit: function(e){ + var context = this.model.toJSON(); + this.$el.html(this.editTemplate(context)); + + return this; + }, + + save: function(e) { + e.preventDefault(); // if there's no changes, do not do anything + + var formData = { + firstName: this.$('#firstName').val().trim(), + lastName: this.$('#lastName').val().trim() + }; + var validate = { + success: function() { + $('#result').addClass('success') + .html('Successfully updated student') + .fadeIn().delay(4000).fadeOut(); + }, + error: function(model, error) { + + } + }; + + this.model.save(formData, validate); + }, + + cancel: function(e) { + e.preventDefault(); // prevent event bubbling + this.render(); } }); From 3e8fd0afd2d307721809554249a90ca139f65f06 Mon Sep 17 00:00:00 2001 From: "frances.go" Date: Sat, 14 Feb 2015 15:13:19 -0800 Subject: [PATCH 3/6] Fix issues with instructor and add Student in main.js --- client/spa/js/instructor/editInstructor.html | 4 ++-- client/spa/js/instructor/instructor.html | 4 ++-- client/spa/js/instructor/instructor.view.js | 10 +++++----- .../spa/js/instructor/spec/instructor.view.spec.js | 8 ++++---- client/spa/js/main.js | 3 ++- client/spa/js/student/editStudent.html | 4 ++-- client/spa/js/student/student.html | 4 ++-- client/spa/js/student/student.view.js | 14 +++++--------- 8 files changed, 24 insertions(+), 27 deletions(-) diff --git a/client/spa/js/instructor/editInstructor.html b/client/spa/js/instructor/editInstructor.html index ca8ff13..b058276 100644 --- a/client/spa/js/instructor/editInstructor.html +++ b/client/spa/js/instructor/editInstructor.html @@ -13,8 +13,8 @@


- - + + diff --git a/client/spa/js/instructor/instructor.html b/client/spa/js/instructor/instructor.html index 7db63c3..cea4e70 100644 --- a/client/spa/js/instructor/instructor.html +++ b/client/spa/js/instructor/instructor.html @@ -14,8 +14,8 @@

<%- firstName %> <%- lastName %>

<%- skills %>

- - + +
diff --git a/client/spa/js/instructor/instructor.view.js b/client/spa/js/instructor/instructor.view.js index eb9bf32..77ef5d6 100644 --- a/client/spa/js/instructor/instructor.view.js +++ b/client/spa/js/instructor/instructor.view.js @@ -12,10 +12,10 @@ module.exports = Backbone.View.extend({ template: _.template(template), editTemplate: _.template(editTemplate), events: { - 'click .i-delete': 'destroy', - 'click .i-edit': 'edit', - 'click .i-save': 'save', - 'click .i-cancel': 'cancel' + 'click .delete': 'destroy', + 'click .modify': 'modify', + 'click .save': 'save', + 'click .cancel': 'cancel' }, initialize: function(){ this.listenTo(this.model, 'destroy', this.remove); @@ -30,7 +30,7 @@ module.exports = Backbone.View.extend({ destroy: function(){ this.model.destroy(); }, - edit: function(e){ + modify: function(e){ var context = this.model.toJSON(); this.$el.html(this.editTemplate(context)); diff --git a/client/spa/js/instructor/spec/instructor.view.spec.js b/client/spa/js/instructor/spec/instructor.view.spec.js index 7b51e47..c383339 100644 --- a/client/spa/js/instructor/spec/instructor.view.spec.js +++ b/client/spa/js/instructor/spec/instructor.view.spec.js @@ -69,7 +69,7 @@ describe('Instructor view ', function(){ // call delegate after spyOn view.delegateEvents(); view.render(); - view.$('.i-edit').trigger('click'); + view.$('.modify').trigger('click'); }); describe('when the user enters new instructor information ', function(){ @@ -77,7 +77,7 @@ describe('Instructor view ', function(){ describe('when user clicks on the cancel button', function(){ beforeEach(function(){ - view.$('.i-cancel').trigger('click'); + view.$('.cancel').trigger('click'); }); it('cancels the user input', function(){ @@ -91,7 +91,7 @@ describe('Instructor view ', function(){ view.$('#lastName').val('changed lastName'); view.$('#skills').val('changed skills'); - view.$('.i-save').trigger('click'); + view.$('.save').trigger('click'); }); it('updates the model', function(){ @@ -120,7 +120,7 @@ describe('Instructor view ', function(){ it('deletes the model', function(){ // Must render for the event to be fired view.render(); - view.$('.i-delete').trigger('click'); + view.$('.delete').trigger('click'); expect(view.destroy).toHaveBeenCalled(); expect(model.destroy).toHaveBeenCalled(); }); // end delete model test diff --git a/client/spa/js/main.js b/client/spa/js/main.js index b1b493f..3b49c64 100644 --- a/client/spa/js/main.js +++ b/client/spa/js/main.js @@ -5,10 +5,11 @@ window.Backbone = require('./vendor').Backbone; // Include your code var Instructor = require('./instructor/instructor.controller'); var Resource = require('./learning-resource/learning-resource.controller'); +var Student = require('./student/student.controller'); // Initialize it window.instructor = new Instructor({router:true, container: 'body'}); window.resource = new Resource({router:true, container: 'body'}); - +window.student = new Student({router:true, container: 'body'}); // Additional modules go here diff --git a/client/spa/js/student/editStudent.html b/client/spa/js/student/editStudent.html index a0a266f..35f55c9 100644 --- a/client/spa/js/student/editStudent.html +++ b/client/spa/js/student/editStudent.html @@ -3,8 +3,8 @@


- - + +
diff --git a/client/spa/js/student/student.html b/client/spa/js/student/student.html index 4fbb221..cc046ab 100644 --- a/client/spa/js/student/student.html +++ b/client/spa/js/student/student.html @@ -1,8 +1,8 @@

<%- firstName %> <%- lastName %>

- - + +
diff --git a/client/spa/js/student/student.view.js b/client/spa/js/student/student.view.js index 0a3b216..eb10f69 100644 --- a/client/spa/js/student/student.view.js +++ b/client/spa/js/student/student.view.js @@ -1,11 +1,8 @@ 'use strict'; - var Backbone = require('../vendor/index').Backbone; var _ = require('../vendor/index')._; var $ = require('../vendor/index').$; - var fs = require('fs'); //will be replaced by brfs in the browser - // readFileSync will be evaluated statically so errors can't be caught var template = fs.readFileSync(__dirname + '/student.html', 'utf8'); var editTemplate = fs.readFileSync(__dirname + '/editStudent.html', 'utf8'); @@ -19,10 +16,10 @@ module.exports = Backbone.View.extend({ editTemplate: _.template(editTemplate), events: { - 'click .s-delete': 'destroy', - 'click .s-edit': 'edit', - 'click .s-save': 'save', - 'click .s-cancel': 'cancel' + 'click .delete': 'destroy', + 'click .modify': 'modify', + 'click .save': 'save', + 'click .cancel': 'cancel' }, initialize: function(){ @@ -40,7 +37,7 @@ module.exports = Backbone.View.extend({ this.model.destroy(); }, - edit: function(e){ + modify: function(e){ var context = this.model.toJSON(); this.$el.html(this.editTemplate(context)); @@ -74,4 +71,3 @@ module.exports = Backbone.View.extend({ } }); - From e2f338ad3011820384cdc63e4999fb5c69b8dd41 Mon Sep 17 00:00:00 2001 From: "frances.go" Date: Sat, 14 Feb 2015 15:19:41 -0800 Subject: [PATCH 4/6] Replace edit with modify for student.view.js --- client/spa/js/student/spec/student.view.spec.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/spa/js/student/spec/student.view.spec.js b/client/spa/js/student/spec/student.view.spec.js index 85e898c..8af96d9 100644 --- a/client/spa/js/student/spec/student.view.spec.js +++ b/client/spa/js/student/spec/student.view.spec.js @@ -68,7 +68,7 @@ describe('student view ', function(){ // call delegate after spyOn view.delegateEvents(); view.render(); - view.$('.s-edit').trigger('click'); + view.$('.modify').trigger('click'); }); describe('when the user enters new student information ', function(){ @@ -76,7 +76,7 @@ describe('student view ', function(){ describe('when user clicks on the cancel button', function(){ beforeEach(function(){ - view.$('.s-cancel').trigger('click'); + view.$('.cancel').trigger('click'); }); it('cancels the user input', function(){ @@ -89,7 +89,7 @@ describe('student view ', function(){ view.$('#firstName').val('changed firstName'); view.$('#lastName').val('changed lastName'); - view.$('.s-save').trigger('click'); + view.$('.save').trigger('click'); }); it('updates the model', function(){ @@ -118,7 +118,7 @@ describe('student view ', function(){ it('deletes the model', function(){ // Must render for the event to be fired view.render(); - view.$('.s-delete').trigger('click'); + view.$('.delete').trigger('click'); expect(view.destroy).toHaveBeenCalled(); expect(model.destroy).toHaveBeenCalled(); }); // end delete model test From 7b035216c16d9cf1cb7c06d9f587c7caf455b920 Mon Sep 17 00:00:00 2001 From: "frances.go" Date: Sun, 15 Feb 2015 23:48:11 -0800 Subject: [PATCH 5/6] Fix save issues with instructor and student --- client/spa/js/instructor/editInstructor.html | 6 +++--- client/spa/js/instructor/instructor.model.js | 2 +- client/spa/js/instructor/instructor.view.js | 21 ++++++++++++++++---- client/spa/js/student/editStudent.html | 4 ++-- client/spa/js/student/student.model.js | 2 +- client/spa/js/student/student.view.js | 20 ++++++++++++++++--- 6 files changed, 41 insertions(+), 14 deletions(-) diff --git a/client/spa/js/instructor/editInstructor.html b/client/spa/js/instructor/editInstructor.html index b058276..e7ce1d8 100644 --- a/client/spa/js/instructor/editInstructor.html +++ b/client/spa/js/instructor/editInstructor.html @@ -10,9 +10,9 @@
-
-
-
+
+
+
diff --git a/client/spa/js/instructor/instructor.model.js b/client/spa/js/instructor/instructor.model.js index 0cae394..61c9a68 100644 --- a/client/spa/js/instructor/instructor.model.js +++ b/client/spa/js/instructor/instructor.model.js @@ -24,6 +24,6 @@ module.exports = Backbone.Model.extend({ if (!attrs.skills){ errors.push('skills cannot be empty'); } - return errors; + return errors.length > 0 ? errors: false; } }); diff --git a/client/spa/js/instructor/instructor.view.js b/client/spa/js/instructor/instructor.view.js index 77ef5d6..e93f400 100644 --- a/client/spa/js/instructor/instructor.view.js +++ b/client/spa/js/instructor/instructor.view.js @@ -44,21 +44,34 @@ module.exports = Backbone.View.extend({ lastName: this.$('#lastName').val().trim(), skills: this.$('#skills').val().trim() }; - var validate = { + var check = { success: function() { $('#result').addClass('success') .html('Successfully updated instructor') .fadeIn().delay(4000).fadeOut(); + this.hideErrors(); // hide if successful for validation array to work }, - error: function(model, error) { - + error: function(model, errors) { + this.showErrors(errors); } }; - this.model.save(formData, validate); + this.model.save(formData, check); }, cancel: function(e) { e.preventDefault(); // prevent event bubbling this.render(); + }, + showErrors: function(errors) { + _.each(errors, function (error) { + var fields = this.$('.' + error.name); + fields.addClass('error'); + fields.find('.help-inline').text(error.message); + }, this); + }, + + hideErrors: function () { + this.$('.textfield').removeClass('error'); + this.$('.help-inline').text(''); } }); diff --git a/client/spa/js/student/editStudent.html b/client/spa/js/student/editStudent.html index 35f55c9..5bb7e59 100644 --- a/client/spa/js/student/editStudent.html +++ b/client/spa/js/student/editStudent.html @@ -1,8 +1,8 @@
-
-
+
+
diff --git a/client/spa/js/student/student.model.js b/client/spa/js/student/student.model.js index a6798ce..c6a23a7 100644 --- a/client/spa/js/student/student.model.js +++ b/client/spa/js/student/student.model.js @@ -24,6 +24,6 @@ module.exports = Backbone.Model.extend({ if (!attrs.lastName){ errors.push('lastName cannot be empty'); } - return errors; + return errors.length > 0 ? errors: false; } }); diff --git a/client/spa/js/student/student.view.js b/client/spa/js/student/student.view.js index eb10f69..f363ab2 100644 --- a/client/spa/js/student/student.view.js +++ b/client/spa/js/student/student.view.js @@ -51,23 +51,37 @@ module.exports = Backbone.View.extend({ firstName: this.$('#firstName').val().trim(), lastName: this.$('#lastName').val().trim() }; - var validate = { + var check = { success: function() { $('#result').addClass('success') .html('Successfully updated student') .fadeIn().delay(4000).fadeOut(); + this.hideErrors(); // hide if successful for validation array to work }, error: function(model, error) { - + this.showErrors(errors); } }; - this.model.save(formData, validate); + this.model.save(formData, check); }, cancel: function(e) { e.preventDefault(); // prevent event bubbling this.render(); + }, + + showErrors: function(errors) { + _.each(errors, function (error) { + var fields = this.$('.' + error.name); + fields.addClass('error'); + fields.find('.help-inline').text(error.message); + }, this); + }, + + hideErrors: function () { + this.$('.textfield').removeClass('error'); + this.$('.help-inline').text(''); } }); From d28b8ffd34cb7661cf1f2c9f6a2bad80c487ad0c Mon Sep 17 00:00:00 2001 From: "frances.go" Date: Sun, 15 Feb 2015 23:53:44 -0800 Subject: [PATCH 6/6] Replace error with errors for student view --- client/spa/js/student/student.view.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/spa/js/student/student.view.js b/client/spa/js/student/student.view.js index f363ab2..e8956ce 100644 --- a/client/spa/js/student/student.view.js +++ b/client/spa/js/student/student.view.js @@ -58,7 +58,7 @@ module.exports = Backbone.View.extend({ .fadeIn().delay(4000).fadeOut(); this.hideErrors(); // hide if successful for validation array to work }, - error: function(model, error) { + error: function(model, errors) { this.showErrors(errors); } };