|
1 | 1 | var _ = require('./index')
|
| 2 | +var config = require('../config') |
2 | 3 | var extend = _.extend
|
3 | 4 |
|
4 | 5 | /**
|
@@ -145,18 +146,17 @@ strats.paramAttributes = function () {
|
145 | 146 | * options and parent options.
|
146 | 147 | */
|
147 | 148 |
|
148 |
| -strats.directives = |
149 |
| -strats.filters = |
150 |
| -strats.transitions = |
151 |
| -strats.components = |
152 |
| -strats.partials = |
153 |
| -strats.elementDirectives = function (parentVal, childVal) { |
| 149 | +function mergeAssets (parentVal, childVal) { |
154 | 150 | var res = Object.create(parentVal)
|
155 | 151 | return childVal
|
156 |
| - ? extend(res, childVal) |
| 152 | + ? extend(res, guardArrayAssets(childVal)) |
157 | 153 | : res
|
158 | 154 | }
|
159 | 155 |
|
| 156 | +config._assetTypes.forEach(function (type) { |
| 157 | + strats[type + 's'] = mergeAssets |
| 158 | +}) |
| 159 | + |
160 | 160 | /**
|
161 | 161 | * Events & Watchers.
|
162 | 162 | *
|
@@ -206,28 +206,87 @@ var defaultStrat = function (parentVal, childVal) {
|
206 | 206 | : childVal
|
207 | 207 | }
|
208 | 208 |
|
| 209 | +/** |
| 210 | + * Merge two option objects into a new one. |
| 211 | + * Core utility used in both instantiation and inheritance. |
| 212 | + * |
| 213 | + * @param {Object} parent |
| 214 | + * @param {Object} child |
| 215 | + * @param {Vue} [vm] - if vm is present, indicates this is |
| 216 | + * an instantiation merge. |
| 217 | + */ |
| 218 | + |
| 219 | +exports.mergeOptions = function merge (parent, child, vm) { |
| 220 | + guardComponents(child) |
| 221 | + guardProps(child) |
| 222 | + var options = {} |
| 223 | + var key |
| 224 | + if (child.mixins) { |
| 225 | + for (var i = 0, l = child.mixins.length; i < l; i++) { |
| 226 | + parent = merge(parent, child.mixins[i], vm) |
| 227 | + } |
| 228 | + } |
| 229 | + for (key in parent) { |
| 230 | + mergeField(key) |
| 231 | + } |
| 232 | + for (key in child) { |
| 233 | + if (!(parent.hasOwnProperty(key))) { |
| 234 | + mergeField(key) |
| 235 | + } |
| 236 | + } |
| 237 | + function mergeField (key) { |
| 238 | + var strat = strats[key] || defaultStrat |
| 239 | + options[key] = strat(parent[key], child[key], vm, key) |
| 240 | + } |
| 241 | + return options |
| 242 | +} |
| 243 | + |
| 244 | +/** |
| 245 | + * Resolve an asset. |
| 246 | + * This function is used because child instances need access |
| 247 | + * to assets defined in its ancestor chain. |
| 248 | + * |
| 249 | + * @param {Object} options |
| 250 | + * @param {String} type |
| 251 | + * @param {String} id |
| 252 | + * @return {Object|Function} |
| 253 | + */ |
| 254 | + |
| 255 | +exports.resolveAsset = function resolve (options, type, id) { |
| 256 | + var asset = options[type][id] |
| 257 | + while (!asset && options._parent) { |
| 258 | + options = options._parent.$options |
| 259 | + asset = options[type][id] |
| 260 | + } |
| 261 | + return asset |
| 262 | +} |
| 263 | + |
209 | 264 | /**
|
210 | 265 | * Make sure component options get converted to actual
|
211 | 266 | * constructors.
|
212 | 267 | *
|
213 |
| - * @param {Object} components |
| 268 | + * @param {Object} options |
214 | 269 | */
|
215 | 270 |
|
216 |
| -function guardComponents (components) { |
217 |
| - if (components) { |
| 271 | +function guardComponents (options) { |
| 272 | + if (options.components) { |
| 273 | + var components = options.components = |
| 274 | + guardArrayAssets(options.components) |
218 | 275 | var def
|
219 |
| - for (var key in components) { |
| 276 | + var ids = Object.keys(components) |
| 277 | + for (var i = 0, l = ids.length; i < l; i++) { |
| 278 | + var key = ids[i] |
220 | 279 | if (_.commonTagRE.test(key)) {
|
221 | 280 | process.env.NODE_ENV !== 'production' && _.warn(
|
222 | 281 | 'Do not use built-in HTML elements as component ' +
|
223 |
| - 'name: ' + key |
| 282 | + 'id: ' + key |
224 | 283 | )
|
225 | 284 | continue
|
226 | 285 | }
|
227 | 286 | def = components[key]
|
228 | 287 | if (_.isPlainObject(def)) {
|
229 |
| - def.name = key |
230 |
| - components[key] = _.Vue.extend(def) |
| 288 | + def.id = def.id || key |
| 289 | + components[key] = def._Ctor || (def._Ctor = _.Vue.extend(def)) |
231 | 290 | }
|
232 | 291 | }
|
233 | 292 | }
|
@@ -261,56 +320,29 @@ function guardProps (options) {
|
261 | 320 | }
|
262 | 321 |
|
263 | 322 | /**
|
264 |
| - * Merge two option objects into a new one. |
265 |
| - * Core utility used in both instantiation and inheritance. |
| 323 | + * Guard an Array-format assets option and converted it |
| 324 | + * into the key-value Object format. |
266 | 325 | *
|
267 |
| - * @param {Object} parent |
268 |
| - * @param {Object} child |
269 |
| - * @param {Vue} [vm] - if vm is present, indicates this is |
270 |
| - * an instantiation merge. |
| 326 | + * @param {Object|Array} assets |
| 327 | + * @return {Object} |
271 | 328 | */
|
272 | 329 |
|
273 |
| -exports.mergeOptions = function merge (parent, child, vm) { |
274 |
| - guardComponents(child.components) |
275 |
| - guardProps(child) |
276 |
| - var options = {} |
277 |
| - var key |
278 |
| - if (child.mixins) { |
279 |
| - for (var i = 0, l = child.mixins.length; i < l; i++) { |
280 |
| - parent = merge(parent, child.mixins[i], vm) |
281 |
| - } |
282 |
| - } |
283 |
| - for (key in parent) { |
284 |
| - mergeField(key) |
285 |
| - } |
286 |
| - for (key in child) { |
287 |
| - if (!(parent.hasOwnProperty(key))) { |
288 |
| - mergeField(key) |
| 330 | +function guardArrayAssets (assets) { |
| 331 | + if (_.isArray(assets)) { |
| 332 | + var res = {} |
| 333 | + var i = assets.length |
| 334 | + var asset |
| 335 | + while (i--) { |
| 336 | + asset = assets[i] |
| 337 | + if (!asset.id) { |
| 338 | + process.env.NODE_ENV !== 'production' && _.warn( |
| 339 | + 'Array-syntax assets must provide an id field.' |
| 340 | + ) |
| 341 | + } else { |
| 342 | + res[asset.id] = asset |
| 343 | + } |
289 | 344 | }
|
| 345 | + return res |
290 | 346 | }
|
291 |
| - function mergeField (key) { |
292 |
| - var strat = strats[key] || defaultStrat |
293 |
| - options[key] = strat(parent[key], child[key], vm, key) |
294 |
| - } |
295 |
| - return options |
296 |
| -} |
297 |
| - |
298 |
| -/** |
299 |
| - * Resolve an asset. |
300 |
| - * This function is used because child instances need access |
301 |
| - * to assets defined in its ancestor chain. |
302 |
| - * |
303 |
| - * @param {Object} options |
304 |
| - * @param {String} type |
305 |
| - * @param {String} id |
306 |
| - * @return {Object|Function} |
307 |
| - */ |
308 |
| - |
309 |
| -exports.resolveAsset = function resolve (options, type, id) { |
310 |
| - var asset = options[type][id] |
311 |
| - while (!asset && options._parent) { |
312 |
| - options = options._parent.$options |
313 |
| - asset = options[type][id] |
314 |
| - } |
315 |
| - return asset |
| 347 | + return assets |
316 | 348 | }
|
0 commit comments