Skip to content

amp-bind: Handle attributes on input elements that sometimes only specify initial values#9584

Merged
kmh287 merged 9 commits intoampproject:masterfrom
kmh287:bind_input_attributes_bug
May 30, 2017
Merged

amp-bind: Handle attributes on input elements that sometimes only specify initial values#9584
kmh287 merged 9 commits intoampproject:masterfrom
kmh287:bind_input_attributes_bug

Conversation

@kmh287
Copy link
Copy Markdown
Contributor

@kmh287 kmh287 commented May 26, 2017

With this PR, if the binding of one of these attributes updates, the actual underlying property will update. For example, the value attribute on a text input only specifies the initial text. If the binding to [value] changes, the text in the input will update.

/to @choumx @jridgewell
/cc @cvializ

Fixes #9556

element.removeAttribute(property);
attributeChanged = true;
}
if (requiresJsUpdate) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can't we move this above the attribute change stuff, and get rid of oldJsPropertyValue?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good idea getting rid of the var, but I'd rather not set attributeChanged until after the attributes have been changed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@jridgewell On second thought, the property value needs to be read before the attributes are changed (in case the user hasn't interacted with the input yet). I've reverted the change. Let me know if you have a cleaner way I'm not seeing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

set attributeChanged until after the attributes have been changed.

I don't see why not? If you do this, it'll work fine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Or you can do:

if (requiresJsUpdate && element[property] !== newValue) {
  element[property] = newValue;
  attributeChanged = true;
}

@kmh287 kmh287 force-pushed the bind_input_attributes_bug branch 2 times, most recently from 7957e37 to cffc5ec Compare May 26, 2017 21:20
} else {
attributeChanged =
(oldValue === null && newValue) ||
(oldValue === '' && !newValue);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think it's more readable if you reverted the attributeChanged = true lines above and removed this else block.

element.removeAttribute(property);
attributeChanged = true;
}
if (requiresJsUpdate) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Or you can do:

if (requiresJsUpdate && element[property] !== newValue) {
  element[property] = newValue;
  attributeChanged = true;
}

// Once the user interacts with these elements, the JS properties
// underlying these attributes must be updated for the change to be
// visible to the user.
const requiresJsUpdate =
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

updateElementProperty?

@kmh287 kmh287 force-pushed the bind_input_attributes_bug branch from 8bd28a5 to 95aa3ab Compare May 26, 2017 21:59
Copy link
Copy Markdown

@dreamofabear dreamofabear left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this!

} else {
attributeChanged =
(oldValue === null && newValue) ||
(oldValue === '' && !newValue);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Still think the old code was easier to read. 😄 Also you'd avoid useless setAttribute/removeAttribute calls on the element.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree, just trying to get the tests to pass before I push 🤣

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

attributeChanged = element[property] !== newValue;
element[property] = newValue;
attributeChanged = true;
} else {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This can't at the same level, as updateElementProperty && value != newValue

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

@kmh287 kmh287 merged commit e42f305 into ampproject:master May 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants