Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #236 from OpenDataPolicingNC/odpm-214--do-not-use-…
Browse files Browse the repository at this point in the history
…memoize-in-abstract-classes

[ODPM-214] Using _.memoize in abstract classes causes weird problems
  • Loading branch information
copelco committed Nov 7, 2018
2 parents 10ae30b + 815e099 commit a898a42
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
24 changes: 12 additions & 12 deletions traffic_stops/static/js/app/common/IncidentByReasonAndRace.js
Expand Up @@ -115,18 +115,18 @@ export const IRRTimeSeriesBase = VisualBase.extend({
},

/***
* Pure function to return array of years present in dataset.
* Function to return array of years present in dataset.
*/
_years: _.memoize(function () {
_years: function () {
var years_all = d3.set(_.pluck(this._raw_data(), 'year')).values();
var years = _.without(years_all, 'Total');
return years;
}),
},

/***
* Pure function to return the dataset sorted by "type" and incident reason.
* Function to return the dataset sorted by "type" and incident reason.
*/
_getByReason: _.memoize(function (reason) {
_getByReason: function (reason) {
var types = this._items();
var years = this._years();
var raw_data, data;
Expand Down Expand Up @@ -170,7 +170,7 @@ export const IRRTimeSeriesBase = VisualBase.extend({
}));

return this._checkThreshold(data);
}),
},

/***
* Helper function to identify data that should not be displayed on
Expand All @@ -195,11 +195,11 @@ export const IRRTimeSeriesBase = VisualBase.extend({
},

/***
* Pure function to create a virtual "All" reason data object for each year.
* Function to create a virtual "All" reason data object for each year.
* Iterates through each year returned by _years and sums up the counts for
* each race/ethnicity.
*/
_reasonAll: _.memoize(function () {
_reasonAll: function () {
var data = [];
var years = this._years();

Expand Down Expand Up @@ -233,7 +233,7 @@ export const IRRTimeSeriesBase = VisualBase.extend({
});

return data;
})
}
});

export const IRRTableBase = TableBase.extend({
Expand All @@ -251,17 +251,17 @@ export const IRRTableBase = TableBase.extend({
this.add_select();
},

_reasons: _.memoize(function () {
_reasons: function () {
return d3.set(_.pluck(this._raw_data(), this.reason_type));
}),
},

add_select: function () {
let div = $(this.get("selector"));

// select input only needs to be added once
if (div.find('select').length) { return true; }

let $selector = $('<select id="srr-table-select">');
let $selector = $(`<select id="${this.get('selector').replace('#', '')}_select">`);
let reasons = this._reasons();
let $opts = [$('<option value="All">All</option>')].concat(
reasons
Expand Down
4 changes: 2 additions & 2 deletions traffic_stops/static/js/app/states/il/StopByReasonAndRace.js
Expand Up @@ -27,13 +27,13 @@ export const SRRTimeSeries = C.IRRTimeSeriesBase.extend({

const SRRTable = C.IRRTableBase.extend({
types: [Stops.ethnicities],

Stops: Stops,
incident_type: 'stop',
incident_type_plural: 'stops',
reason_type: 'purpose',
reason_order_key: 'purpose_order',

Stops: Stops,

_get_header_rows: function () {
return Stops.ethnicities
},
Expand Down
6 changes: 5 additions & 1 deletion traffic_stops/static/js/app/states/nc/StopByReasonAndRace.js
Expand Up @@ -35,7 +35,11 @@ const SRRTable = C.IRRTableBase.extend({
reason_order_key: 'purpose_order',

_get_header_rows: function () {
return Stops.ethnicities
return Stops.pprint.values();
},

_pprint: function (type) {
return Stops.pprint.get(type);
},

_raw_data: function () {
Expand Down

0 comments on commit a898a42

Please sign in to comment.