Skip to content

Commit 2429b37

Browse files
committed
subclasses can now use asset registration methods too
1 parent 35967cb commit 2429b37

File tree

5 files changed

+72
-84
lines changed

5 files changed

+72
-84
lines changed

src/compiler.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,11 +627,12 @@ CompilerProto.markComputed = function (binding, value) {
627627
*/
628628
CompilerProto.getOption = function (type, id) {
629629
var opts = this.options,
630-
parent = this.parentCompiler
630+
parent = this.parentCompiler,
631+
globalAssets = config.globalAssets
631632
return (opts[type] && opts[type][id]) || (
632633
parent
633634
? parent.getOption(type, id)
634-
: utils[type] && utils[type][id]
635+
: globalAssets[type] && globalAssets[type][id]
635636
)
636637
}
637638

src/main.js

Lines changed: 38 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
11
var config = require('./config'),
22
ViewModel = require('./viewmodel'),
3-
directives = require('./directives'),
4-
filters = require('./filters'),
5-
utils = require('./utils')
3+
utils = require('./utils'),
4+
makeHash = utils.hash,
5+
assetTypes = ['directive', 'filter', 'partial', 'transition', 'component']
6+
7+
ViewModel.options = config.globalAssets = {
8+
directives : require('./directives'),
9+
filters : require('./filters'),
10+
partials : makeHash(),
11+
transitions : makeHash(),
12+
components : makeHash()
13+
}
14+
15+
/**
16+
* Expose asset registration methods
17+
*/
18+
assetTypes.forEach(function (type) {
19+
ViewModel[type] = function (id, value) {
20+
var hash = this.options[type + 's']
21+
if (!hash) {
22+
hash = this.options[type + 's'] = makeHash()
23+
}
24+
if (!value) return hash[id]
25+
if (type === 'partial') {
26+
value = utils.toFragment(value)
27+
} else if (type === 'component') {
28+
value = utils.toConstructor(value)
29+
}
30+
hash[id] = value
31+
return this
32+
}
33+
})
634

735
/**
836
* Set config options
@@ -20,51 +48,6 @@ ViewModel.config = function (opts, val) {
2048
return this
2149
}
2250

23-
/**
24-
* Allows user to register/retrieve a directive definition
25-
*/
26-
ViewModel.directive = function (id, fn) {
27-
if (!fn) return directives[id]
28-
directives[id] = fn
29-
return this
30-
}
31-
32-
/**
33-
* Allows user to register/retrieve a filter function
34-
*/
35-
ViewModel.filter = function (id, fn) {
36-
if (!fn) return filters[id]
37-
filters[id] = fn
38-
return this
39-
}
40-
41-
/**
42-
* Allows user to register/retrieve a ViewModel constructor
43-
*/
44-
ViewModel.component = function (id, Ctor) {
45-
if (!Ctor) return utils.components[id]
46-
utils.components[id] = utils.toConstructor(Ctor)
47-
return this
48-
}
49-
50-
/**
51-
* Allows user to register/retrieve a template partial
52-
*/
53-
ViewModel.partial = function (id, partial) {
54-
if (!partial) return utils.partials[id]
55-
utils.partials[id] = utils.toFragment(partial)
56-
return this
57-
}
58-
59-
/**
60-
* Allows user to register/retrieve a transition definition object
61-
*/
62-
ViewModel.transition = function (id, transition) {
63-
if (!transition) return utils.transitions[id]
64-
utils.transitions[id] = transition
65-
return this
66-
}
67-
6851
/**
6952
* Expose internal modules for plugins
7053
*/
@@ -133,6 +116,12 @@ function extend (options) {
133116
ExtendedVM.extend = extend
134117
ExtendedVM.super = ParentVM
135118
ExtendedVM.options = options
119+
120+
// allow extended VM to add its own assets
121+
assetTypes.forEach(function (type) {
122+
ExtendedVM[type] = ViewModel[type]
123+
})
124+
136125
return ExtendedVM
137126
}
138127

@@ -150,7 +139,7 @@ function extend (options) {
150139
* extension option, but only as an instance option.
151140
*/
152141
function inheritOptions (child, parent, topLevel) {
153-
child = child || utils.hash()
142+
child = child || makeHash()
154143
if (!parent) return child
155144
for (var key in parent) {
156145
if (key === 'el' || key === 'methods') continue

src/utils.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,15 @@ var defer =
1212
window.webkitRequestAnimationFrame ||
1313
window.setTimeout
1414

15-
/**
16-
* Create a prototype-less object
17-
* which is a better hash/map
18-
*/
19-
function makeHash () {
20-
return Object.create(null)
21-
}
22-
2315
var utils = module.exports = {
2416

25-
hash: makeHash,
26-
27-
// global storage for user-registered
28-
// vms, partials and transitions
29-
components : makeHash(),
30-
partials : makeHash(),
31-
transitions : makeHash(),
17+
/**
18+
* Create a prototype-less object
19+
* which is a better hash/map
20+
*/
21+
hash: function () {
22+
return Object.create(null)
23+
},
3224

3325
/**
3426
* get an attribute and remove it.

test/functional/fixtures/extend.html

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,6 @@
2626
data: {
2727
vmMsg: 'component works'
2828
}
29-
},
30-
'vm-w-model': {
31-
data : {
32-
selfMsg: 'component with model '
33-
}
34-
}
35-
},
36-
partials: {
37-
'partial-test': '{{partialMsg}}'
38-
},
39-
directives: {
40-
hola: function (value) {
41-
this.el.innerHTML = value + ' works'
4229
}
4330
},
4431
filters: {
@@ -47,6 +34,16 @@
4734
}
4835
}
4936
})
37+
// test late asset addition
38+
T.directive('hola', function (value) {
39+
this.el.innerHTML = value + ' works'
40+
})
41+
T.partial('partial-test', '{{partialMsg}}')
42+
T.component('vm-w-model', {
43+
data : {
44+
selfMsg: 'component with model '
45+
}
46+
})
5047
var C = T.extend({
5148
created: function () {
5249
log.textContent += ' C created'

test/unit/specs/api.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
describe('UNIT: API', function () {
22

33
var utils = require('vue/src/utils'),
4+
assets = require('vue/src/config').globalAssets,
45
nextTick = utils.nextTick
56

67
describe('config()', function () {
@@ -176,12 +177,12 @@ describe('UNIT: API', function () {
176177

177178
it('should register a Component constructor', function () {
178179
Vue.component(testId, Test)
179-
assert.strictEqual(utils.components[testId], Test)
180+
assert.strictEqual(assets.components[testId], Test)
180181
})
181182

182183
it('should also work with option objects', function () {
183184
Vue.component(testId2, opts)
184-
assert.ok(utils.components[testId2].prototype instanceof Vue)
185+
assert.ok(assets.components[testId2].prototype instanceof Vue)
185186
})
186187

187188
it('should retrieve the VM if has only one arg', function () {
@@ -212,14 +213,14 @@ describe('UNIT: API', function () {
212213

213214
it('should register the partial as a dom fragment', function () {
214215
Vue.partial(testId, partial)
215-
var converted = utils.partials[testId]
216+
var converted = assets.partials[testId]
216217
assert.ok(converted instanceof window.DocumentFragment)
217218
assert.strictEqual(converted.querySelector('.partial-test a').innerHTML, '{{hi}}')
218219
assert.strictEqual(converted.querySelector('span').innerHTML, 'hahaha')
219220
})
220221

221222
it('should retrieve the partial if has only one arg', function () {
222-
assert.strictEqual(utils.partials[testId], Vue.partial(testId))
223+
assert.strictEqual(assets.partials[testId], Vue.partial(testId))
223224
})
224225

225226
it('should work with v-partial as a directive', function () {
@@ -254,7 +255,7 @@ describe('UNIT: API', function () {
254255

255256
it('should register a transition object', function () {
256257
Vue.transition(testId, transition)
257-
assert.strictEqual(utils.transitions[testId], transition)
258+
assert.strictEqual(assets.transitions[testId], transition)
258259
})
259260

260261
it('should retrieve the transition if has only one arg', function () {
@@ -341,6 +342,14 @@ describe('UNIT: API', function () {
341342
assert.notOk(child.test3.hi)
342343
})
343344

345+
it('should allow subclasses to attach private assets', function () {
346+
var Sub = Vue.extend({})
347+
Sub.component('test', {})
348+
assert.strictEqual(Sub.options.components.test.super, Vue)
349+
Sub.partial('test', '123')
350+
assert.ok(Sub.options.partials.test instanceof window.DocumentFragment)
351+
})
352+
344353
describe('Options', function () {
345354

346355
describe('methods', function () {

0 commit comments

Comments
 (0)