Skip to content

Commit a86dd14

Browse files
committed
use null hash for directives and filters as well
1 parent 0f3b2e4 commit a86dd14

File tree

12 files changed

+276
-225
lines changed

12 files changed

+276
-225
lines changed

src/compiler.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ var Emitter = require('./emitter'),
1111

1212
// cache methods
1313
slice = [].slice,
14-
makeHash = utils.hash,
1514
extend = utils.extend,
1615
hasOwn = ({}).hasOwnProperty,
1716
def = Object.defineProperty,
@@ -46,23 +45,23 @@ function Compiler (vm, options) {
4645
compiler.destroyed = false
4746

4847
// process and extend options
49-
options = compiler.options = options || makeHash()
48+
options = compiler.options = options || {}
5049
utils.processOptions(options)
5150

5251
// copy compiler options
5352
extend(compiler, options.compilerOptions)
5453
// repeat indicates this is a v-repeat instance
5554
compiler.repeat = compiler.repeat || false
5655
// expCache will be shared between v-repeat instances
57-
compiler.expCache = compiler.expCache || makeHash()
56+
compiler.expCache = compiler.expCache || {}
5857

5958
// initialize element
6059
var el = compiler.el = compiler.setupElement(options)
6160
utils.log('\nnew VM instance: ' + el.tagName + '\n')
6261

6362
// set other compiler properties
6463
compiler.vm = el.vue_vm = vm
65-
compiler.bindings = makeHash()
64+
compiler.bindings = utils.hash()
6665
compiler.dirs = []
6766
compiler.deferred = []
6867
compiler.computed = []
@@ -86,7 +85,7 @@ function Compiler (vm, options) {
8685
// VM ---------------------------------------------------------------------
8786

8887
// set VM properties
89-
vm.$ = makeHash()
88+
vm.$ = {}
9089
vm.$el = el
9190
vm.$options = options
9291
vm.$compiler = compiler
@@ -258,7 +257,7 @@ CompilerProto.setupObserver = function () {
258257

259258
// a hash to hold event proxies for each root level key
260259
// so they can be referenced and removed later
261-
observer.proxies = makeHash()
260+
observer.proxies = {}
262261
observer._ctx = compiler.vm
263262

264263
// add own listeners which trigger binding updates

src/directives/html.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
var guard = require('../utils').guard,
22
slice = [].slice
33

4+
/**
5+
* Binding for innerHTML
6+
*/
47
module.exports = {
58

69
bind: function () {

src/directives/if.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
var utils = require('../utils')
22

3+
/**
4+
* Manages a conditional child VM
5+
*/
36
module.exports = {
47

58
bind: function () {

src/directives/index.js

Lines changed: 111 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,129 @@
11
var utils = require('../utils'),
22
config = require('../config'),
3-
transition = require('../transition')
3+
transition = require('../transition'),
4+
directives = module.exports = utils.hash()
45

5-
module.exports = {
6-
7-
on : require('./on'),
8-
repeat : require('./repeat'),
9-
model : require('./model'),
10-
'if' : require('./if'),
11-
'with' : require('./with'),
12-
html : require('./html'),
13-
style : require('./style'),
14-
partial : require('./partial'),
15-
view : require('./view'),
16-
17-
component : {
18-
isLiteral: true,
19-
bind: function () {
20-
if (!this.el.vue_vm) {
21-
this.childVM = new this.Ctor({
22-
el: this.el,
23-
parent: this.vm
24-
})
25-
}
26-
},
27-
unbind: function () {
28-
if (this.childVM) {
29-
this.childVM.$destroy()
30-
}
6+
/**
7+
* Nest and manage a Child VM
8+
*/
9+
directives.component = {
10+
isLiteral: true,
11+
bind: function () {
12+
if (!this.el.vue_vm) {
13+
this.childVM = new this.Ctor({
14+
el: this.el,
15+
parent: this.vm
16+
})
3117
}
3218
},
33-
34-
attr: {
35-
bind: function () {
36-
var params = this.vm.$options.paramAttributes
37-
this.isParam = params && params.indexOf(this.arg) > -1
38-
},
39-
update: function (value) {
40-
if (value || value === 0) {
41-
this.el.setAttribute(this.arg, value)
42-
} else {
43-
this.el.removeAttribute(this.arg)
44-
}
45-
if (this.isParam) {
46-
this.vm[this.arg] = utils.checkNumber(value)
47-
}
19+
unbind: function () {
20+
if (this.childVM) {
21+
this.childVM.$destroy()
4822
}
49-
},
23+
}
24+
}
5025

51-
text: {
52-
bind: function () {
53-
this.attr = this.el.nodeType === 3
54-
? 'nodeValue'
55-
: 'textContent'
56-
},
57-
update: function (value) {
58-
this.el[this.attr] = utils.guard(value)
59-
}
26+
/**
27+
* Binding HTML attributes
28+
*/
29+
directives.attr = {
30+
bind: function () {
31+
var params = this.vm.$options.paramAttributes
32+
this.isParam = params && params.indexOf(this.arg) > -1
6033
},
34+
update: function (value) {
35+
if (value || value === 0) {
36+
this.el.setAttribute(this.arg, value)
37+
} else {
38+
this.el.removeAttribute(this.arg)
39+
}
40+
if (this.isParam) {
41+
this.vm[this.arg] = utils.checkNumber(value)
42+
}
43+
}
44+
}
6145

62-
show: function (value) {
63-
var el = this.el,
64-
target = value ? '' : 'none',
65-
change = function () {
66-
el.style.display = target
67-
}
68-
transition(el, value ? 1 : -1, change, this.compiler)
46+
/**
47+
* Binding textContent
48+
*/
49+
directives.text = {
50+
bind: function () {
51+
this.attr = this.el.nodeType === 3
52+
? 'nodeValue'
53+
: 'textContent'
6954
},
55+
update: function (value) {
56+
this.el[this.attr] = utils.guard(value)
57+
}
58+
}
7059

71-
'class': function (value) {
72-
if (this.arg) {
73-
utils[value ? 'addClass' : 'removeClass'](this.el, this.arg)
74-
} else {
75-
if (this.lastVal) {
76-
utils.removeClass(this.el, this.lastVal)
77-
}
78-
if (value) {
79-
utils.addClass(this.el, value)
80-
this.lastVal = value
81-
}
60+
/**
61+
* Binding CSS display property
62+
*/
63+
directives.show = function (value) {
64+
var el = this.el,
65+
target = value ? '' : 'none',
66+
change = function () {
67+
el.style.display = target
8268
}
83-
},
69+
transition(el, value ? 1 : -1, change, this.compiler)
70+
}
8471

85-
cloak: {
86-
isEmpty: true,
87-
bind: function () {
88-
var el = this.el
89-
this.compiler.observer.once('hook:ready', function () {
90-
el.removeAttribute(config.prefix + '-cloak')
91-
})
72+
/**
73+
* Binding CSS classes
74+
*/
75+
directives['class'] = function (value) {
76+
if (this.arg) {
77+
utils[value ? 'addClass' : 'removeClass'](this.el, this.arg)
78+
} else {
79+
if (this.lastVal) {
80+
utils.removeClass(this.el, this.lastVal)
9281
}
93-
},
82+
if (value) {
83+
utils.addClass(this.el, value)
84+
this.lastVal = value
85+
}
86+
}
87+
}
88+
89+
/**
90+
* Only removed after the owner VM is ready
91+
*/
92+
directives.cloak = {
93+
isEmpty: true,
94+
bind: function () {
95+
var el = this.el
96+
this.compiler.observer.once('hook:ready', function () {
97+
el.removeAttribute(config.prefix + '-cloak')
98+
})
99+
}
100+
}
94101

95-
ref: {
96-
isLiteral: true,
97-
bind: function () {
98-
var id = this.expression
99-
if (id) {
100-
this.vm.$parent.$[id] = this.vm
101-
}
102-
},
103-
unbind: function () {
104-
var id = this.expression
105-
if (id) {
106-
delete this.vm.$parent.$[id]
107-
}
102+
/**
103+
* Store a reference to self in parent VM's $
104+
*/
105+
directives.ref = {
106+
isLiteral: true,
107+
bind: function () {
108+
var id = this.expression
109+
if (id) {
110+
this.vm.$parent.$[id] = this.vm
111+
}
112+
},
113+
unbind: function () {
114+
var id = this.expression
115+
if (id) {
116+
delete this.vm.$parent.$[id]
108117
}
109118
}
119+
}
110120

111-
}
121+
directives.on = require('./on')
122+
directives.repeat = require('./repeat')
123+
directives.model = require('./model')
124+
directives['if'] = require('./if')
125+
directives['with'] = require('./with')
126+
directives.html = require('./html')
127+
directives.style = require('./style')
128+
directives.partial = require('./partial')
129+
directives.view = require('./view')

src/directives/model.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ function getMultipleSelectOptions (select) {
1515
})
1616
}
1717

18+
/**
19+
* Two-way binding for form input elements
20+
*/
1821
module.exports = {
1922

2023
bind: function () {

src/directives/on.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
var utils = require('../utils')
22

3+
/**
4+
* Binding for event listeners
5+
*/
36
module.exports = {
47

58
isFn: true,

src/directives/partial.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
var utils = require('../utils')
22

3+
/**
4+
* Binding for partials
5+
*/
36
module.exports = {
47

58
isLiteral: true,

src/directives/repeat.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
var utils = require('../utils'),
22
config = require('../config')
33

4+
/**
5+
* Binding that manages VMs based on an Array
6+
*/
47
module.exports = {
58

69
bind: function () {

src/directives/style.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ function camelReplacer (m) {
55
return m[1].toUpperCase()
66
}
77

8+
/**
9+
* Binding for CSS styles
10+
*/
811
module.exports = {
912

1013
bind: function () {

src/directives/view.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Manages a conditional child VM using the
3+
* binding's value as the component ID.
4+
*/
15
module.exports = {
26

37
bind: function () {

0 commit comments

Comments
 (0)