aboutsummaryrefslogtreecommitdiff
path: root/node_modules/vue/src/core/observer/array.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/vue/src/core/observer/array.js')
-rw-r--r--node_modules/vue/src/core/observer/array.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/node_modules/vue/src/core/observer/array.js b/node_modules/vue/src/core/observer/array.js
new file mode 100644
index 00000000..684e37ec
--- /dev/null
+++ b/node_modules/vue/src/core/observer/array.js
@@ -0,0 +1,45 @@
+/*
+ * not type checking this file because flow doesn't play well with
+ * dynamically accessing methods on Array prototype
+ */
+
+import { def } from '../util/index'
+
+const arrayProto = Array.prototype
+export const arrayMethods = Object.create(arrayProto)
+
+const methodsToPatch = [
+ 'push',
+ 'pop',
+ 'shift',
+ 'unshift',
+ 'splice',
+ 'sort',
+ 'reverse'
+]
+
+/**
+ * Intercept mutating methods and emit events
+ */
+methodsToPatch.forEach(function (method) {
+ // cache original method
+ const original = arrayProto[method]
+ def(arrayMethods, method, function mutator (...args) {
+ const result = original.apply(this, args)
+ const ob = this.__ob__
+ let inserted
+ switch (method) {
+ case 'push':
+ case 'unshift':
+ inserted = args
+ break
+ case 'splice':
+ inserted = args.slice(2)
+ break
+ }
+ if (inserted) ob.observeArray(inserted)
+ // notify change
+ ob.dep.notify()
+ return result
+ })
+})