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

fix #8093: Use custom toString method when interpolating objects if implemented #8217

Merged
merged 5 commits into from
Dec 21, 2018

Conversation

mathieutu
Copy link
Contributor

@mathieutu mathieutu commented May 22, 2018

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Code style update
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change? (check one)

  • Yes
  • No

The PR fulfills these requirements:

Description:

Hi,
When interpolating an object, it uses the JSON.stringify method instead of toString, which is normally the proper method to set a way to convert an object to a string.
This PR allow you to use your custom toString method to render your object if it exists. It's tested (I've also added a test for toJson), and it should not be a breaking change.

Thanks you for your amazing work folks, and see you in Paris next month!

@mathieutu
Copy link
Contributor Author

I'm adding some example of the problem to make me understood better:

Edit Vue Template

@posva:

I can confirm that your (so used!) Object.create(null) example works exactly the same than before (like arrays), as it's not a "plain object" (_toString(Object.create(null)) gives [object Undefined]).

And to write here what I told you yesterday, it could be a BC for those who had a object with a toString method implemented and still want to use the toJson in templates. I think we can easily consider that this is not a standard case. 😅

See you! 👋

@posva posva changed the title fix #8093: Use custom toString method in object rendering. fix #8093: Use custom toString method when interpolating objects if implemented Jul 1, 2018
@@ -77,7 +77,7 @@ export function isValidArrayIndex (val: any): boolean {
export function toString (val: any): string {
return val == null
? ''
: typeof val === 'object'
: typeof val === 'object' && (!isPlainObject(val) || val.toString === _toString)
Copy link
Member

Choose a reason for hiding this comment

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

I think this function can really use early returns now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As you want, I kept the original style to make as few changes as possible and let the diff easier to read, but it'll be easy to refactor.

@yyx990803 yyx990803 changed the base branch from dev to 2.6 December 21, 2018 18:12
@yyx990803 yyx990803 merged commit 0e4e45e into vuejs:2.6 Dec 21, 2018
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.

4 participants