Skip to content

Array observation when modified by Array.prototype.splice #2673

@GuillaumeLeclerc

Description

@GuillaumeLeclerc

Vue.js version

1.0.21

Reproduction Link

http://www.webpackbin.com/4JE1cYtyW

Steps to reproduce

  • watch an array by putting it in the data property
  • Make the template display the array
  • remove an element using Array.prototype.splice.call(this.arr, 0)

What is Expected?

  • The first element of the array is removed from the view

What is actually happening?

  • The first element is removed from the array but not from the view

Explanation

I read the source code from the observing feature of vue. As far as I understood Vue intercept calls to methods. Using Array.prototype make this strategy fail (it would work with the dirty check in Angular 1.x for example). I know it will not be easily fixable nor it's a good practice to use Array.prototype. But actually it's how lodash is implemented (I found this "bug" when using lodash) And I this kind of library is very widely used.

Intent to fix

Maybe we could fix it by doing this ?

const originalSplice = Array.prototype.splice;

Array.prototype.splice = function() {
  if (this && this.length) {
    this.splice(arguments)
  } else {
    originalSplice.apply(this, arguments)
  }
} 

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions