Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Encounters collection is sortable by the name of patient or provider

  • Loading branch information...
commit 4bc484ef1d0c05ebedd1749810b840bbbc11f970 1 parent 9deab8b
@chiku authored
View
4 public/index.dev.html
@@ -78,8 +78,8 @@
<script type="text/template" id="template-encounters">
<h2>List of patients</h2>
<nav id="encounters-sort">
- <a href="#" data-sort-by="name">Name</a>
- <a href="#" data-sort-by="id">ID</a>
+ <a href="#" data-sort-by="patientName">Patient</a>
+ <a href="#" data-sort-by="providerName">Provider</a>
</nav>
<ol id="encounters-container"></ol>
</script>
View
4 public/index.prod.html
@@ -52,8 +52,8 @@
<script type="text/template" id="template-encounters">
<h2>List of patients</h2>
<nav id="encounters-sort">
- <a href="#" data-sort-by="name">Name</a>
- <a href="#" data-sort-by="id">ID</a>
+ <a href="#" data-sort-by="patientName">Patient</a>
+ <a href="#" data-sort-by="providerName">Provider</a>
</nav>
<ol id="encounters-container"></ol>
</script>
View
13 public/javascripts/app/collections/encounters.collection.js
@@ -17,15 +17,24 @@ Application.Collections.Encounters = Backbone.Collection.extend({
},
comparator: function(encounter) {
- return encounter.get("name");
+ return encounter.patientName();
},
reorderBy: function(field) {
+ if (!this._allowedReorderOn(field)) {
+ return this;
+ }
+
this.comparator = function(encounter) {
- return encounter.get(field);
+ return encounter[field]();
};
+
this.sort();
return this;
+ },
+
+ _allowedReorderOn: function(field) {
+ return _(["patientName", "providerName"]).include(field);
}
});
View
15 public/javascripts/app/models/encounter.js
@@ -2,4 +2,17 @@
/*jslint white: false, nomen: false */
/*global Application: true, Backbone: true, _: true */
-Application.Models.Encounter = Backbone.Model.extend({ });
+Application.Models.Encounter = Backbone.Model.extend({
+ patientName: function(encounter) {
+ return this._retrivePropertyFor('patient');
+ },
+
+ providerName: function(encounter) {
+ return this._retrivePropertyFor('provider');
+ },
+
+ _retrivePropertyFor: function(property) {
+ var person = this.get(property);
+ return person ? person.display : undefined;
+ }
+});
View
22 public/javascripts/app/views/encounters.sort.js
@@ -2,17 +2,17 @@
/*jslint white: false, nomen: false */
/*global Application: true, Backbone: true, _: true, jQuery: true */
-jQuery(function() {
- Application.Views.Encounters.Sort = Backbone.View.extend({
- el: jQuery(Application.Configuration.Selectors.encountersSort),
+Application.Views.Encounters.Sort = Backbone.View.extend({
+ el: function() {
+ return jQuery(Application.Configuration.Selectors.encountersSort)
+ },
- events: {
- 'click': 'sortPatients'
- },
+ events: {
+ 'click': 'sortPatients'
+ },
- sortPatients: function(e) {
- var property = jQuery(e.target).attr('data-sort-by');
- this.collection.reorderBy(property);
- }
- });
+ sortPatients: function(e) {
+ var property = jQuery(e.target).attr('data-sort-by');
+ this.collection.reorderBy(property);
+ }
});
View
76 spec/javascripts/collections/encounters.collection.spec.js
@@ -7,44 +7,62 @@ describe("Encounters collection", function() {
});
describe("is ordered", function() {
- var encounterOne = new Application.Models.Encounter({
- id: 2,
- name: "Xyz Abc",
- doctor_name: "Doctor A"
- });
- var encounterTwo = new Application.Models.Encounter({
- id: 3,
- name: "Abc Xyz",
- doctor_name: "Doctor C"
- });
- var encounterThree = new Application.Models.Encounter({
- id: 1,
- name: "ABc",
- doctor_name: "Doctor B"
+ beforeEach(function() {
+ this.encounterOne = new Application.Models.Encounter({
+ patient: { display: "Xyz Abc" },
+ provider: { display: "Doctor A" },
+ id: "a2"
+ });
+ this.encounterTwo = new Application.Models.Encounter({
+ patient: { display: "Abc Xyz" },
+ provider: { display: "Doctor C" },
+ id: "a3"
+ });
+ this.encounterThree = new Application.Models.Encounter({
+ patient: { display: "ABc" },
+ provider: { display: "Doctor B" },
+ id: "a1"
+ });
+ this.encounters = new Application.Collections.Encounters();
+ this.encounters.add([this.encounterOne, this.encounterTwo, this.encounterThree]);
});
- var encounters = new Application.Collections.Encounters();
- encounters.add([encounterOne, encounterTwo, encounterThree]);
- it("by name by default", function() {
- expect(encounters.at(0)).toBe(encounterThree);
- expect(encounters.at(1)).toBe(encounterTwo);
- expect(encounters.at(2)).toBe(encounterOne);
+ it("by patient name be default", function() {
+ expect(this.encounters.at(0)).toBe(this.encounterThree);
+ expect(this.encounters.at(1)).toBe(this.encounterTwo);
+ expect(this.encounters.at(2)).toBe(this.encounterOne);
});
describe("on a re-order", function() {
- it("by doctor_name", function() {
- encounters.reorderBy('doctor_name');
- expect(encounters.at(0)).toBe(encounterOne);
- expect(encounters.at(1)).toBe(encounterThree);
- expect(encounters.at(2)).toBe(encounterTwo);
+ it("by provider name", function() {
+ this.encounters.reorderBy('providerName');
+ expect(this.encounters.at(0)).toBe(this.encounterOne);
+ expect(this.encounters.at(1)).toBe(this.encounterThree);
+ expect(this.encounters.at(2)).toBe(this.encounterTwo);
+ });
+
+ it("by patient name", function() {
+ this.encounters.reorderBy('patientName');
+ expect(this.encounters.at(0)).toBe(this.encounterThree);
+ expect(this.encounters.at(1)).toBe(this.encounterTwo);
+ expect(this.encounters.at(2)).toBe(this.encounterOne);
+ });
+
+ it("by incorrect filed is no-op", function() {
+ this.encounters.reorderBy('providerName');
+ this.encounters.reorderBy('BadField');
+ this.encounters.reorderBy('VeryBadField');
+ expect(this.encounters.at(0)).toBe(this.encounterOne);
+ expect(this.encounters.at(1)).toBe(this.encounterThree);
+ expect(this.encounters.at(2)).toBe(this.encounterTwo);
});
});
it("after multiple re-orders", function(){
- encounters.reorderBy('doctor_name').reorderBy('name');
- expect(encounters.at(0)).toBe(encounterThree);
- expect(encounters.at(1)).toBe(encounterTwo);
- expect(encounters.at(2)).toBe(encounterOne);
+ this.encounters.reorderBy('providerName').reorderBy('patientName');
+ expect(this.encounters.at(0)).toBe(this.encounterThree);
+ expect(this.encounters.at(1)).toBe(this.encounterTwo);
+ expect(this.encounters.at(2)).toBe(this.encounterOne);
});
});
View
1  spec/javascripts/index.dev.html
@@ -34,6 +34,7 @@
<script src="code/app/views/encounters.sort.js"></script>
<!-- include spec files here... -->
+ <script src="models/encounter.spec.js"></script>
<script src="collections/encounters.collection.spec.js"></script>
<script src="router.spec.js"></script>
View
1  spec/javascripts/index.prod.html
@@ -22,6 +22,7 @@
<script src="code/all.min.js"></script>
<!-- include spec files here... -->
+ <script src="models/encounter.spec.js"></script>
<script src="collections/encounters.collection.spec.js"></script>
<script src="router.spec.js"></script>
View
37 spec/javascripts/models/encounter.spec.js
@@ -0,0 +1,37 @@
+describe("Encounter", function() {
+ describe("which is well-formed", function() {
+ beforeEach(function() {
+ this.encounter = new Application.Models.Encounter({
+ patient: { display: "Xyz Abc" },
+ provider: { display: "Doctor A" },
+ id: "a2"
+ });
+ });
+
+ it("knows the name of the patient", function() {
+ expect(this.encounter.patientName()).toBe("Xyz Abc");
+ });
+
+ it("knows the name of the provider", function() {
+ expect(this.encounter.providerName()).toBe("Doctor A");
+ });
+ });
+
+ describe("which is mal-formed", function() {
+ beforeEach(function() {
+ this.encounter = new Application.Models.Encounter({
+ patient: { random: "Xyz Abc" },
+ noProvider: { display: "Doctor A" },
+ id: "a2"
+ });
+ });
+
+ it("has undefined patient name", function() {
+ expect(this.encounter.patientName()).toBeUndefined();
+ });
+
+ it("has undefined provider name", function() {
+ expect(this.encounter.providerName()).toBeUndefined();
+ });
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.