Skip to content

Commit 81af134

Browse files
committed
feat: Add improved <list-view> syntax: for="item in items"
1 parent 2fcfec9 commit 81af134

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import class_ from './class'
22
import style from './style'
3-
import scopedSlots from './scopedSlots'
3+
import listView from './list-view'
4+
import vTemplate from './v-template'
45
import view from './view'
56

6-
export default [class_, style, scopedSlots, view]
7+
export default [class_, style, vTemplate, listView, view]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { normalizeElementName } from '../../element-registry'
2+
import { parseFor } from 'compiler/parser/index'
3+
import { getAndRemoveAttr, addRawAttr } from 'compiler/helpers'
4+
5+
function preTransformNode(el) {
6+
if (normalizeElementName(el.tag) !== 'listview') {
7+
return
8+
}
9+
const exp = getAndRemoveAttr(el, 'for')
10+
if (!exp) return
11+
12+
const res = parseFor(exp)
13+
if (!res) return
14+
15+
addRawAttr(el, ':items', res.for)
16+
addRawAttr(el, '+alias', res.alias)
17+
}
18+
19+
export default {
20+
preTransformNode
21+
}

platform/nativescript/runtime/components/list-view.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,8 @@ class ItemContext {
9191
this.even = index % 2 === 0
9292
this.odd = !this.even
9393
}
94+
95+
toString() {
96+
return this.value && this.value.toString()
97+
}
9498
}

platform/nativescript/runtime/components/v-template.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export default {
1313

1414
if: {
1515
type: String
16+
},
17+
18+
'+alias': {
19+
type: String,
20+
default: 'item'
1621
}
1722
},
1823

@@ -26,6 +31,7 @@ export default {
2631
this.$templates.registerTemplate(
2732
this.$props.name || (this.$props.if ? `v-template-${tid++}` : 'default'),
2833
this.$props.if,
34+
this.$props['+alias'],
2935
this.$scopedSlots.default
3036
)
3137
},
@@ -38,10 +44,11 @@ export class TemplateBag {
3844
this._templateMap = new Map()
3945
}
4046

41-
registerTemplate(name, condition, scopedFn) {
47+
registerTemplate(name, condition, alias, scopedFn) {
4248
this._templateMap.set(name, {
4349
condition,
44-
conditionFn: this.getConditionFn(condition),
50+
alias,
51+
conditionFn: this.getConditionFn(condition, alias),
4552
scopedFn,
4653
keyedTemplate: new VueKeyedTemplate(name, scopedFn)
4754
})
@@ -62,8 +69,8 @@ export class TemplateBag {
6269
}
6370
}
6471

65-
getConditionFn(condition) {
66-
return new Function('item', `return !!(${condition})`)
72+
getConditionFn(condition, alias) {
73+
return new Function(alias, `return !!(${condition})`)
6774
}
6875

6976
getKeyedTemplate(name) {

platform/nativescript/runtime/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Vue.prototype.$mount = function(el, hydrating) {
8686
return mount.call(this, el, hydrating)
8787
}
8888

89+
Vue.compile = compileToFunctions
8990
Vue.prototype.$renderTemplate = function(template, context, oldVnode) {
9091
let slot = template
9192
if (typeof template !== 'function') {

0 commit comments

Comments
 (0)