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

Allow named properties on reactive arrays. #5216

Merged
merged 3 commits into from
Mar 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/core/observer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export function defineReactive (
* already exist.
*/
export function set (target: Array<any> | Object, key: any, val: any): any {
if (Array.isArray(target)) {
if (Array.isArray(target) && typeof key === 'number') {
target.length = Math.max(target.length, key)
target.splice(key, 1, val)
return val
Expand All @@ -198,7 +198,7 @@ export function set (target: Array<any> | Object, key: any, val: any): any {
target[key] = val
return val
}
const ob = target.__ob__
const ob = (target : any).__ob__
if (target._isVue || (ob && ob.vmCount)) {
process.env.NODE_ENV !== 'production' && warn(
'Avoid adding reactive properties to a Vue instance or its root $data ' +
Expand All @@ -219,11 +219,11 @@ export function set (target: Array<any> | Object, key: any, val: any): any {
* Delete a property and trigger change if necessary.
*/
export function del (target: Array<any> | Object, key: any) {
if (Array.isArray(target)) {
if (Array.isArray(target) && typeof key === 'number') {
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.

if (target._isVue || (ob && ob.vmCount)) {
process.env.NODE_ENV !== 'production' && warn(
'Avoid deleting properties on a Vue instance or its root $data ' +
Expand Down
11 changes: 11 additions & 0 deletions test/unit/modules/observer/observer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,17 @@ describe('Observer', () => {
delProp(obj3, 'a')
expect(hasOwn(obj3, 'a')).toBe(false)
expect(dep3.notify.calls.count()).toBe(2)
// set and delete non-numeric key on array
const arr2 = ['a']
const ob2 = observe(arr2)
const dep2 = ob2.dep
spyOn(dep2, 'notify')
setProp(arr2, 'b', 2)
expect(arr2.b).toBe(2)
expect(dep2.notify.calls.count()).toBe(1)
delProp(arr2, 'b')
expect(hasOwn(arr2, 'b')).toBe(false)
expect(dep2.notify.calls.count()).toBe(2)
})

it('warning set/delete on a Vue instance', done => {
Expand Down