Skip to content

Commit 3575ff4

Browse files
committed
wip: codegen for scoped slots
1 parent 87186bf commit 3575ff4

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

flow/compiler.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ declare type ASTElement = {
8787
transitionMode?: string | null;
8888
slotName?: ?string;
8989
slotTarget?: ?string;
90+
scopedSlots?: { [name: string]: ASTElement };
9091

9192
ref?: string;
9293
refInFor?: boolean;

src/compiler/codegen/index.js

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,11 @@ function genData (el: ASTElement): string {
182182
}
183183
// inline-template
184184
if (el.inlineTemplate) {
185-
const ast = el.children[0]
186-
if (process.env.NODE_ENV !== 'production' && (
187-
el.children.length > 1 || ast.type !== 1
188-
)) {
189-
warn('Inline-template components must have exactly one child element.')
190-
}
191-
if (ast.type === 1) {
192-
const inlineRenderFns = generate(ast, currentOptions)
193-
data += `inlineTemplate:{render:function(){${
194-
inlineRenderFns.render
195-
}},staticRenderFns:[${
196-
inlineRenderFns.staticRenderFns.map(code => `function(){${code}}`).join(',')
197-
}]}`
198-
}
185+
data += `${genInlineTemplate(el)},`
186+
}
187+
// scoped slots
188+
if (el.scopedSlots) {
189+
data += `${genScopedSlots(el.scopedSlots)},`
199190
}
200191
data = data.replace(/,$/, '') + '}'
201192
// v-bind data wrap
@@ -236,6 +227,39 @@ function genDirectives (el: ASTElement): string | void {
236227
}
237228
}
238229

230+
function genInlineTemplate (el) {
231+
const ast = el.children[0]
232+
if (process.env.NODE_ENV !== 'production' && (
233+
el.children.length > 1 || ast.type !== 1
234+
)) {
235+
warn('Inline-template components must have exactly one child element.')
236+
}
237+
if (ast.type === 1) {
238+
const inlineRenderFns = generate(ast, currentOptions)
239+
return `inlineTemplate:{render:function(){${
240+
inlineRenderFns.render
241+
}},staticRenderFns:[${
242+
inlineRenderFns.staticRenderFns.map(code => `function(){${code}}`).join(',')
243+
}]}`
244+
} else {
245+
return ''
246+
}
247+
}
248+
249+
function genScopedSlots (slots) {
250+
return `scopedSlots:{${
251+
Object.keys(slots).map(key => genScopedSlot(key, slots[key])).join(',')
252+
}}`
253+
}
254+
255+
function genScopedSlot (key: string, el: ASTElement) {
256+
return `${key}:function(${String(el.attrsMap.scope)}){` +
257+
`return ${el.tag === 'template'
258+
? genChildren(el) || 'void 0'
259+
: genElement(el)
260+
}}`
261+
}
262+
239263
function genChildren (el: ASTElement): string | void {
240264
if (el.children.length) {
241265
return '[' + el.children.map(genNode).join(',') + ']'

src/compiler/parser/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,10 @@ export function parse (
167167
}
168168
}
169169
if (currentParent && !element.forbidden) {
170-
if (element.else) {
170+
if (element.else) { // else block
171171
processElse(element, currentParent)
172+
} else if (element.slotTarget && element.attrsMap.scope) { // scoped slot
173+
(currentParent.scopedSlots || (currentParent.scopedSlots = {}))[element.slotTarget] = element
172174
} else {
173175
currentParent.children.push(element)
174176
element.parent = currentParent

0 commit comments

Comments
 (0)