Skip to content

Commit 4faf16e

Browse files
committed
rename Binding -> Dep
1 parent 57d81ef commit 4faf16e

19 files changed

+111
-113
lines changed

component.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"src/api/global.js",
1919
"src/api/lifecycle.js",
2020
"src/batcher.js",
21-
"src/binding.js",
2221
"src/cache.js",
2322
"src/compiler/compile.js",
2423
"src/compiler/transclude.js",
@@ -53,6 +52,7 @@
5352
"src/instance/init.js",
5453
"src/instance/scope.js",
5554
"src/observer/array.js",
55+
"src/observer/dep.js",
5656
"src/observer/index.js",
5757
"src/observer/object.js",
5858
"src/parsers/directive.js",

src/api/lifecycle.js

-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ function cleanup (vm) {
125125
vm.$parent =
126126
vm.$root =
127127
vm._children =
128-
vm._bindings =
129128
vm._directives = null
130129
// call the last hook...
131130
vm._isDestroyed = true

src/instance/scope.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var _ = require('../util')
22
var Observer = require('../observer')
3-
var Binding = require('../binding')
3+
var Dep = require('../observer/dep')
44

55
/**
66
* Setup the scope of an instance, which contains:
@@ -197,20 +197,20 @@ exports._initMeta = function () {
197197
*/
198198

199199
exports._defineMeta = function (key, value) {
200-
var binding = new Binding()
200+
var dep = new Dep()
201201
Object.defineProperty(this, key, {
202202
enumerable: true,
203203
configurable: true,
204204
get: function metaGetter () {
205205
if (Observer.target) {
206-
Observer.target.addDep(binding)
206+
Observer.target.addDep(dep)
207207
}
208208
return value
209209
},
210210
set: function metaSetter (val) {
211211
if (val !== value) {
212212
value = val
213-
binding.notify()
213+
dep.notify()
214214
}
215215
}
216216
})

src/binding.js src/observer/dep.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
var uid = 0
22

33
/**
4-
* A binding is an observable that can have multiple
4+
* A dep is an observable that can have multiple
55
* directives subscribing to it.
66
*
77
* @constructor
88
*/
99

10-
function Binding () {
10+
function Dep () {
1111
this.id = ++uid
1212
this.subs = []
1313
}
1414

15-
var p = Binding.prototype
15+
var p = Dep.prototype
1616

1717
/**
1818
* Add a directive subscriber.
@@ -47,4 +47,4 @@ p.notify = function () {
4747
}
4848
}
4949

50-
module.exports = Binding
50+
module.exports = Dep

src/observer/index.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var _ = require('../util')
22
var config = require('../config')
3-
var Binding = require('../binding')
3+
var Dep = require('./dep')
44
var arrayMethods = require('./array')
55
var arrayKeys = Object.getOwnPropertyNames(arrayMethods)
66
require('./object')
@@ -58,7 +58,7 @@ function Observer (value, type) {
5858
this.id = ++uid
5959
this.value = value
6060
this.active = true
61-
this.bindings = []
61+
this.deps = []
6262
_.define(value, '__ob__', this)
6363
if (type === ARRAY) {
6464
var augment = config.proto && _.hasProto
@@ -126,10 +126,10 @@ p.walk = function (obj) {
126126

127127
/**
128128
* Try to carete an observer for a child value,
129-
* and if value is array, link binding to the array.
129+
* and if value is array, link dep to the array.
130130
*
131131
* @param {*} val
132-
* @return {Binding|undefined}
132+
* @return {Dep|undefined}
133133
*/
134134

135135
p.observe = function (val) {
@@ -160,9 +160,9 @@ p.observeArray = function (items) {
160160
p.convert = function (key, val) {
161161
var ob = this
162162
var childOb = ob.observe(val)
163-
var binding = new Binding()
163+
var dep = new Dep()
164164
if (childOb) {
165-
childOb.bindings.push(binding)
165+
childOb.deps.push(dep)
166166
}
167167
Object.defineProperty(ob.value, key, {
168168
enumerable: true,
@@ -171,40 +171,40 @@ p.convert = function (key, val) {
171171
// Observer.target is a watcher whose getter is
172172
// currently being evaluated.
173173
if (ob.active && Observer.target) {
174-
Observer.target.addDep(binding)
174+
Observer.target.addDep(dep)
175175
}
176176
return val
177177
},
178178
set: function (newVal) {
179179
if (newVal === val) return
180-
// remove binding from old value
180+
// remove dep from old value
181181
var oldChildOb = val && val.__ob__
182182
if (oldChildOb) {
183-
var oldBindings = oldChildOb.bindings
184-
oldBindings.splice(oldBindings.indexOf(binding), 1)
183+
var oldDeps = oldChildOb.deps
184+
oldDeps.splice(oldDeps.indexOf(dep), 1)
185185
}
186186
val = newVal
187-
// add binding to new value
187+
// add dep to new value
188188
var newChildOb = ob.observe(newVal)
189189
if (newChildOb) {
190-
newChildOb.bindings.push(binding)
190+
newChildOb.deps.push(dep)
191191
}
192-
binding.notify()
192+
dep.notify()
193193
}
194194
})
195195
}
196196

197197
/**
198-
* Notify change on all self bindings on an observer.
198+
* Notify change on all self deps on an observer.
199199
* This is called when a mutable value mutates. e.g.
200200
* when an Array's mutating methods are called, or an
201201
* Object's $add/$delete are called.
202202
*/
203203

204204
p.notify = function () {
205-
var bindings = this.bindings
206-
for (var i = 0, l = bindings.length; i < l; i++) {
207-
bindings[i].notify()
205+
var deps = this.deps
206+
for (var i = 0, l = deps.length; i < l; i++) {
207+
deps[i].notify()
208208
}
209209
}
210210

src/watcher.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@ function Watcher (vm, expression, cb, filters, needSet, deep) {
4646
var p = Watcher.prototype
4747

4848
/**
49-
* Add a binding dependency to this directive.
49+
* Add a dependency to this directive.
5050
*
51-
* @param {Binding} binding
51+
* @param {Dep} dep
5252
*/
5353

54-
p.addDep = function (binding) {
55-
var id = binding.id
54+
p.addDep = function (dep) {
55+
var id = dep.id
5656
if (!this.newDeps[id]) {
57-
this.newDeps[id] = binding
57+
this.newDeps[id] = dep
5858
if (!this.deps[id]) {
59-
this.deps[id] = binding
60-
binding.addSub(this)
59+
this.deps[id] = dep
60+
dep.addSub(this)
6161
}
6262
}
6363
}

test/unit/specs/api/lifecycle_spec.js

-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ if (_.inBrowser) {
168168
expect(vm.$parent).toBeNull()
169169
expect(vm.$root).toBeNull()
170170
expect(vm._children).toBeNull()
171-
expect(vm._bindings).toBeNull()
172171
expect(vm._directives).toBeNull()
173172
expect(Object.keys(vm._events).length).toBe(0)
174173
})

test/unit/specs/binding_spec.js

-34
This file was deleted.

test/unit/specs/filters_spec.js test/unit/specs/filters/filters_spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var Vue = require('../../../src/vue')
2-
var filters = require('../../../src/filters')
1+
var Vue = require('../../../../src/vue')
2+
var filters = require('../../../../src/filters')
33

44
describe('Filters', function () {
55

test/unit/specs/observer/dep_spec.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var Dep = require('../../../../src/observer/dep')
2+
3+
describe('Dep', function () {
4+
5+
var d
6+
beforeEach(function () {
7+
d = new Dep()
8+
})
9+
10+
it('addSub', function () {
11+
var sub = {}
12+
d.addSub(sub)
13+
expect(d.subs.length).toBe(1)
14+
expect(d.subs.indexOf(sub)).toBe(0)
15+
})
16+
17+
it('removeSub', function () {
18+
var sub = {}
19+
d.addSub(sub)
20+
d.removeSub(sub)
21+
expect(d.subs.length).toBe(0)
22+
expect(d.subs.indexOf(sub)).toBe(-1)
23+
})
24+
25+
it('notify', function () {
26+
var sub = {
27+
update: jasmine.createSpy('sub')
28+
}
29+
d.addSub(sub)
30+
d.notify()
31+
expect(sub.update).toHaveBeenCalled()
32+
})
33+
34+
})

0 commit comments

Comments
 (0)