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

Be able to delete array item in Vue.delete #4747

Merged
merged 3 commits into from
Jan 19, 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
6 changes: 5 additions & 1 deletion src/core/observer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@ export function set (obj: Array<any> | Object, key: any, val: any) {
/**
* Delete a property and trigger change if necessary.
*/
export function del (obj: Object, key: string) {
export function del (obj: Array<any> | Object, key: any) {
Copy link
Member

Choose a reason for hiding this comment

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

key is either a string or a number, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I just keep the type check be same with Vue.set

Copy link
Member

Choose a reason for hiding this comment

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

I see, in that case, we should probably update both

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The conflict is the first argument of splice must be number, can't be string.

Copy link
Member

Choose a reason for hiding this comment

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

I must be missing something 😅 what is wrong with key: number | string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have tried, if I set the key: number | string, the type check of obj.splice(key, 1) would be failed. Because the first argument of splice must be a number.

if (Array.isArray(obj)) {
obj.splice(key, 1)
return
}
const ob = obj.__ob__
if (obj._isVue || (ob && ob.vmCount)) {
process.env.NODE_ENV !== 'production' && warn(
Expand Down
24 changes: 24 additions & 0 deletions test/unit/features/global-api/set-delete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,29 @@ describe('Global API: set/delete', () => {
expect(vm.$el.innerHTML).toBe('')
}).then(done)
})

it('be able to delete an item in array', done => {
Copy link
Member

Choose a reason for hiding this comment

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

blank line here please :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I will also remove the blank line after .$mount()

const vm = new Vue({
template: '<div><p v-for="obj in lists">{{obj.name}}</p></div>',
data: {
lists: [
{ name: 'A' },
{ name: 'B' },
{ name: 'C' }
]
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<p>A</p><p>B</p><p>C</p>')
Vue.delete(vm.lists, 1)
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('<p>A</p><p>C</p>')
Vue.delete(vm.lists, 1)
}).then(() => {
expect(vm.$el.innerHTML).toBe('<p>A</p>')
Vue.delete(vm.lists, 0)
}).then(() => {
expect(vm.$el.innerHTML).toBe('')
}).then(done)
})
})
})