From 78779c258d12afd01f063aced5832148239cf0ba Mon Sep 17 00:00:00 2001 From: Hasan Kumar Reddy A Date: Sat, 20 Feb 2016 11:12:07 +0530 Subject: [PATCH] Make markup changes idempotent (on a ruby on rails application with turbolinks) When I press browser back and forward, the initialization code runs again creating double elements --- scripts/material.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/scripts/material.js b/scripts/material.js index 7b8e7466..a66f1ee0 100644 --- a/scripts/material.js +++ b/scripts/material.js @@ -82,8 +82,9 @@ // Add fake-checkbox to material checkboxes var $input = $((selector) ? selector : this.options.checkboxElements) .filter(":notmdproc") - .data("mdproc", true) - .after(""); + .data("mdproc", true); + if ($input.siblings(".checkbox-material").length == 0) + $input.after(""); _toggleTypeFocus($input); }, @@ -91,8 +92,9 @@ // Add fake-checkbox to material checkboxes var $input = $((selector) ? selector : this.options.togglebuttonElements) .filter(":notmdproc") - .data("mdproc", true) - .after(""); + .data("mdproc", true); + if ($input.siblings(".toggle").length == 0) + $input.after(""); _toggleTypeFocus($input); }, @@ -100,8 +102,9 @@ // Add fake-radio to material radios var $input = $((selector) ? selector : this.options.radioElements) .filter(":notmdproc") - .data("mdproc", true) - .after(""); + .data("mdproc", true); + if ($input.siblings(".circle").length == 0) + $input.after(""); _toggleTypeFocus($input); }, @@ -156,7 +159,8 @@ } // Add at the end of the form-group - $formGroup.append(""); + if ($formGroup.siblings(".material-input").length == 0) + $formGroup.append(""); // Support for file input if ($formGroup.find("input[type=file]").length > 0) {