Skip to content

Allow named properties on reactive arrays. #5216

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

Merged
merged 3 commits into from
Mar 23, 2017

Conversation

pkaminski
Copy link
Contributor

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

If yes, please describe the impact and migration path for existing applications:

The PR fulfills these requirements:

If adding a new feature, the PR's description includes:

  • A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it)

Other information:

Since arrays can be treated as normal objects in JavaScript and contain a mix of numeric and named properties, this patch makes Vue.set and Vue.del deal correctly with named properties on reactive arrays.

Without the cast to any, flow complains that Array doesn't have an
__ob__ property.  This appears to be an instance of this issue:
facebook/flow#1330
target.splice(key, 1)
return
}
const ob = target.__ob__
const ob = (target : any).__ob__
Copy link
Member

Choose a reason for hiding this comment

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

I can't understand this change. Could you explain?

Copy link
Contributor Author

@pkaminski pkaminski Mar 20, 2017

Choose a reason for hiding this comment

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

I can't say I understand it either, but without this cast Flow complains that Array doesn't have an __ob__ property -- why it decided that target is an array, I don't know. It appears to be an instance of this Flow issue, though my knowledge of Flow is minimal so I can't tell for sure.

@yyx990803
Copy link
Member

I'm not sure if this is a good idea - custom properties on Arrays are ignored during JSON.stringify and cannot be persisted. I personally would never rely on them.

@pkaminski
Copy link
Contributor Author

I'm confused -- what do reactive properties have to do with persistence? I certainly don't do this often myself, but it's occasionally useful to pass metadata through an API/SPI where you control both ends but can only transmit an array through the library. IMHO the principle that you can safely replace any member assignment with a call to Vue.set outweighs trying to "fix" iffy JavaScript semantics.

@yyx990803
Copy link
Member

@pkaminski yeah, I guess it doesn't matter that much. I'm just thinking about the implications this has to do with normal persistence scenarios and in particular Vuex snapshots.

@pkaminski
Copy link
Contributor Author

Honestly, I don't even care if the resulting named array property is reactive (though I think it should be for consistency), I'd just like Vue.set not to crash when invoked with an array target and a string key.

@yyx990803 yyx990803 merged commit e47b1e5 into vuejs:dev Mar 23, 2017
@pkaminski
Copy link
Contributor Author

Thanks!

awamwang pushed a commit to awamwang/vue that referenced this pull request Jun 15, 2017
* Allow named properties on reactive arrays.

* Remove semicolons to comport with style guide.

* Pacify flow type checking.

Without the cast to any, flow complains that Array doesn't have an
__ob__ property.  This appears to be an instance of this issue:
facebook/flow#1330
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.

3 participants