Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report-a-problem JS refactoring #581

Merged
merged 11 commits into from
Apr 23, 2015
Merged

Report-a-problem JS refactoring #581

merged 11 commits into from
Apr 23, 2015

Conversation

benilovj
Copy link
Contributor

This work is done in preparation to implementing an alternative design.

This was worked on by @binaryberry, @fofr and myself.

benilovj and others added 8 commits April 22, 2015 16:04
`window.GOVUK.reportAProblem` has been split into a `ReportAProblem` class
(which is responsible for the entire report-a-problem widget) and a
`ReportAProblemForm`, which is scoped to just the form. This refactoring
makes the code less procedural.
Remove last use of `reportAProblem`
Split ReportAProblemForm and ReportAProblem into two files for clarity.
* Don’t concatenate string, use `\` line break
* Move `var` declaration to top
* Keep quotes consistent
Improve readability of module
The error and confirmation messages affect HTML beyond the scope of the
form module, instead trigger events and allow ReportAProblem module to
determine how to handle these situations.

* Also use smart quotes for error message
Improve readability and make it clear that there’s an ‘on submit’
listener.
@benilovj
Copy link
Contributor Author

/cc @dsingleton

url: "/contact/govuk/problem_reports",
dataType: "json",
data: this.$form.serialize(),
success: $.proxy(this.triggerSuccess, this),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to use proxy over Function.prototype.bind? It would be good to move everything to bind where possible for consistency. In this case the bind polyfill is already loaded by the time this code will be run.

@edds
Copy link
Contributor

edds commented Apr 23, 2015

Added a minor comment about the use of proxy over bind.

I also think the new structure of ReportAProblem is slightly confusing. It looks like a prototypal object but the only method which is exposed on the created object works like a static method rather than an instance method. The two other methods which, which would be valid instance methods, showConfirmation and showError are also hidden away in the constructors closure. I think it would be a good idea to go further so the object is more like:

function ReportAProblem(){}
ReportAProblem.prototype.addToggleLink = function(){
  ...
  this.$container...
}
ReportAProblem.prototype.showConfirmation = function(){
  ...
  this.$container...
}
ReportAProblem.prototype.showError = function(){
  ...
  this.$container...
}

This would then also better match the structure and interface you have used for the new ReportAProblemForm object.

@benilovj
Copy link
Contributor Author

@edds I've addressed your comments with c6bb0cf and 47e1398

$toggle.on("click", ".report-a-problem-toggle a", function(evt) {
$container.toggle();
evt.preventDefault();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can loose the argument to this method and make it work like an instance method using something like:

ReportAProblem.prototype.addToggleLink = function() {
  var $toggle = $('\
    <div class="report-a-problem-toggle-wrapper js-footer">\
      <p class="report-a-problem-toggle">\
        <a href="">Is there anything wrong with this page?</a>\
      </p>\
    </div>');

  this.$container.before($toggle);
  $toggle.on("click", ".report-a-problem-toggle a", function(evt) {
    this.$container.toggle();
    evt.preventDefault();
  }.bind(this));
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in de79558

edds added a commit that referenced this pull request Apr 23, 2015
@edds edds merged commit fb6ffeb into master Apr 23, 2015
@edds edds deleted the report-a-problem-refactoring branch April 23, 2015 11:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants