Skip to content

Commit

Permalink
Fix rename scenario for Internet Explorer
Browse files Browse the repository at this point in the history
Using _.curry appeared to call the validate function before it needed to
be called, which resulted in a JS error and prevented the modal from
being displayed. Keeping the correct scope for the validate function is
slightly reworked here so that _.curry does not need to be used, which
lets things work as expected in IE.

Also, update tests for new function signature.

Refs #2315
  • Loading branch information
caseycesari committed Oct 3, 2017
1 parent 08d314a commit a45dba5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/mmw/js/src/modeling/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -977,20 +977,20 @@ var ScenariosCollection = Backbone.Collection.extend({
},

/** Validate the new scenario name
@param model - the model your trying to rename
The value of *this* is the scenario model.
@param newName the new name string
@returns If valid, null
If invalid, a string with the error
**/
validateNewScenarioName: function(model, newName) {
validateNewScenarioName: function(newName) {
var trimmedNewName = newName.trim();

// Bail early if the name actually didn't change.
if (model.get('name') === trimmedNewName) {
if (this.get('name') === trimmedNewName) {
return null;
}

var match = this.find(function(model) {
var match = this.collection.find(function(model) {
return model.get('name').toLowerCase() === trimmedNewName.toLowerCase();
});

Expand Down
13 changes: 7 additions & 6 deletions src/mmw/js/src/modeling/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -921,24 +921,25 @@ describe('Modeling', function() {
describe('#validateNewScenarioName', function() {
it ('returns null if valid rename', function() {
var collection = getTestScenarioCollection(),
validationMessage = collection.validateNewScenarioName(collection.at(1),
'My New Unique Scenario Name');
boundFunc = _.bind(collection.validateNewScenarioName, collection.at(1)),
validationMessage = boundFunc('My New Unique Scenario Name');

assert.equal(validationMessage, null);
});

it('ignores case when comparing the new name with existing names', function() {
var collection = getTestScenarioCollection(),
validationMessage = collection.validateNewScenarioName(collection.at(1),
'cUrreNt condiTIONS');
boundFunc = _.bind(collection.validateNewScenarioName, collection.at(1)),
validationMessage = boundFunc('cUrreNt condiTIONS');

assert.notEqual(validationMessage, null);
});

it('will not show error when leaving name as is', function() {
var collection = getTestScenarioCollection(),
validationMessage = collection.validateNewScenarioName(collection.at(1),
'New Scenario 1');
boundFunc = _.bind(collection.validateNewScenarioName, collection.at(1)),
validationMessage = boundFunc('New Scenario 1');

assert.equal(validationMessage, null);
});
});
Expand Down
5 changes: 2 additions & 3 deletions src/mmw/js/src/modeling/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,14 +457,13 @@ var ScenarioDropDownMenuOptionsView = Marionette.ItemView.extend({
renameScenario: function() {
var self = this,
collection = self.model.collection,
validate = _.bind(collection.validateNewScenarioName, collection),
curriedValidationFunction = _.curry(validate)(this.model),
validate = _.bind(collection.validateNewScenarioName, self.model),
rename = new modalViews.InputView({
model: new modalModels.InputModel({
initial: this.model.get('name'),
title: 'Rename Scenario',
fieldLabel: 'Scenario Name',
validationFunction: curriedValidationFunction,
validationFunction: validate,
})
});

Expand Down

0 comments on commit a45dba5

Please sign in to comment.