1
1
var config = require ( './config' ) ,
2
2
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
+ } )
6
34
7
35
/**
8
36
* Set config options
@@ -20,51 +48,6 @@ ViewModel.config = function (opts, val) {
20
48
return this
21
49
}
22
50
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
-
68
51
/**
69
52
* Expose internal modules for plugins
70
53
*/
@@ -133,6 +116,12 @@ function extend (options) {
133
116
ExtendedVM . extend = extend
134
117
ExtendedVM . super = ParentVM
135
118
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
+
136
125
return ExtendedVM
137
126
}
138
127
@@ -150,7 +139,7 @@ function extend (options) {
150
139
* extension option, but only as an instance option.
151
140
*/
152
141
function inheritOptions ( child , parent , topLevel ) {
153
- child = child || utils . hash ( )
142
+ child = child || makeHash ( )
154
143
if ( ! parent ) return child
155
144
for ( var key in parent ) {
156
145
if ( key === 'el' || key === 'methods' ) continue
0 commit comments