Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support v-model in weex #4178

Merged
merged 6 commits into from
Nov 12, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[wip] supported v-model in Weex
  • Loading branch information
Jinjiang committed Nov 10, 2016
commit 116d7d9d7be634f6218c5dcc5c09a9ff3db295c6
84 changes: 84 additions & 0 deletions src/compiler/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,87 @@ export function getAndRemoveAttr (el: ASTElement, name: string): ?string {
}
return val
}

let len, str, chr, index, expressionPos, expressionEndPos

/**
* parse directive model to do the array update transform. a[idx] = val => $$a.splice($$idx, 1, val)
*
* for loop possible cases:
*
* - test
* - test[idx]
* - test[test1[idx]]
* - test["a"][idx]
* - xxx.test[a[a].test1[idx]]
* - test.xxx.a["asa"][test1[idx]]
*
*/

export function parseModel (val: string): Object {
str = val
len = str.length
index = expressionPos = expressionEndPos = 0

if (val.indexOf('[') < 0) {
return {
exp: val,
idx: null
}
}

while (!eof()) {
chr = next()
/* istanbul ignore if */
if (isStringStart(chr)) {
parseString(chr)
} else if (chr === 0x5B) {
parseBracket(chr)
}
}

return {
exp: val.substring(0, expressionPos),
idx: val.substring(expressionPos + 1, expressionEndPos)
}
}

function next (): number {
return str.charCodeAt(++index)
}

function eof (): boolean {
return index >= len
}

function isStringStart (chr: number): boolean {
return chr === 0x22 || chr === 0x27
}

function parseBracket (chr: number): void {
let inBracket = 1
expressionPos = index
while (!eof()) {
chr = next()
if (isStringStart(chr)) {
parseString(chr)
continue
}
if (chr === 0x5B) inBracket++
if (chr === 0x5D) inBracket--
if (inBracket === 0) {
expressionEndPos = index
break
}
}
}

function parseString (chr: number): void {
const stringQuote = chr
while (!eof()) {
chr = next()
if (chr === stringQuote) {
break
}
}
}
3 changes: 1 addition & 2 deletions src/platforms/web/compiler/directives/model.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* @flow */

import { isIE } from 'core/util/env'
import { addHandler, addProp, getBindingAttr } from 'compiler/helpers'
import parseModel from 'web/util/model'
import { addHandler, addProp, getBindingAttr, parseModel } from 'compiler/helpers'

let warn

Expand Down
85 changes: 0 additions & 85 deletions src/platforms/web/util/model.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/platforms/weex/compiler/directives/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
import model from './model'

export default {
model
}
40 changes: 40 additions & 0 deletions src/platforms/weex/compiler/directives/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* @flow */

import { addHandler, addProp, parseModel } from 'compiler/helpers'

let warn

export default function model (
el: ASTElement,
dir: ASTDirective,
_warn: Function
): ?boolean {
warn = _warn
genDefaultModel(el, dir.value, dir.modifiers)
return true
}

function genDefaultModel (
el: ASTElement,
value: string,
modifiers: ?ASTModifiers
): ?boolean {
const type = el.attrsMap.type
const { lazy } = modifiers || {}
const event = lazy ? 'change' : 'input'
const code = genAssignmentCode(value, `$event`)
addProp(el, 'value', `(${value})`)
addHandler(el, event, code, null, true)
}

function genAssignmentCode (value: string, assignment: string): string {
const modelRs = parseModel(value)
if (modelRs.idx === null) {
return `${value}=${assignment}`
} else {
return `var $$exp = ${modelRs.exp}, $$idx = ${modelRs.idx};` +
`if (!Array.isArray($$exp)){` +
`${value}=${assignment}}` +
`else{$$exp.splice($$idx, 1, ${assignment})}`
}
}