Skip to content

Commit 93b2cf9

Browse files
author
Evan You
committed
custom elements first pass
1 parent 5f9b61b commit 93b2cf9

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

src/compiler.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ CompilerProto.compile = function (node, root) {
220220
// special attributes to check
221221
var repeatExp,
222222
componentId,
223-
partialId
223+
partialId,
224+
customElementFn = utils.elements[node.tagName.toLowerCase()]
224225

225226
// It is important that we access these attributes
226227
// procedurally because the order matters.
@@ -239,21 +240,16 @@ CompilerProto.compile = function (node, root) {
239240
compiler.bindDirective(directive)
240241
}
241242

242-
// sd-component has second highest priority
243-
// and we preseve all other attributes as well.
243+
// custom elements has 2nd highest priority
244+
} else if (customElementFn) {
245+
246+
addChild(customElementFn)
247+
248+
// sd-component has 3rd highest priority
244249
} else if (!root && (componentId = utils.attr(node, 'component'))) {
245250

246251
var ChildVM = compiler.getOption('components', componentId)
247-
if (ChildVM) {
248-
var child = new ChildVM({
249-
el: node,
250-
child: true,
251-
compilerOptions: {
252-
parentCompiler: compiler
253-
}
254-
})
255-
compiler.childCompilers.push(child.$compiler)
256-
}
252+
if (ChildVM) addChild(ChildVM)
257253

258254
} else {
259255

@@ -279,6 +275,22 @@ CompilerProto.compile = function (node, root) {
279275
compiler.compileTextNode(node)
280276

281277
}
278+
279+
function addChild (Ctor) {
280+
if (utils.isConstructor(Ctor)) {
281+
var child = new Ctor({
282+
el: node,
283+
child: true,
284+
compilerOptions: {
285+
parentCompiler: compiler
286+
}
287+
})
288+
compiler.childCompilers.push(child.$compiler)
289+
} else {
290+
// simply call the function
291+
Ctor(node)
292+
}
293+
}
282294
}
283295

284296
/**

src/main.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ ViewModel.component = function (id, Ctor) {
4242
return this
4343
}
4444

45+
/**
46+
* Allows user to register/retrieve a Custom element constructor
47+
*/
48+
ViewModel.element = function (id, Ctor) {
49+
if (!Ctor) return utils.elements[id]
50+
utils.elements[id] = utils.toConstructor(Ctor)
51+
return this
52+
}
53+
4554
/**
4655
* Allows user to register/retrieve a template partial
4756
*/

src/utils.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var utils = module.exports = {
2222
components : makeHash(),
2323
partials : makeHash(),
2424
transitions : makeHash(),
25+
elements : makeHash(),
2526

2627
/**
2728
* get an attribute and remove it.
@@ -132,10 +133,17 @@ var utils = module.exports = {
132133
* if it is not already one
133134
*/
134135
toConstructor: function (obj) {
136+
ViewModel = ViewModel || require('./viewmodel')
137+
return utils.typeOf(obj) === 'Object'
138+
? ViewModel.extend(obj)
139+
: typeof obj === 'function'
140+
? obj
141+
: null
142+
},
143+
144+
isConstructor: function (obj) {
135145
ViewModel = ViewModel || require('./viewmodel')
136146
return obj.prototype instanceof ViewModel || obj === ViewModel
137-
? obj
138-
: ViewModel.extend(obj)
139147
},
140148

141149
/**

0 commit comments

Comments
 (0)