Skip to content

Commit

Permalink
feat(cli): added input "v-mode" support
Browse files Browse the repository at this point in the history
  • Loading branch information
Gcaufy committed Jul 31, 2018
1 parent c16e085 commit 514b1a1
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 12 deletions.
14 changes: 13 additions & 1 deletion packages/core/weapp/init/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const eventHandler = function (method, fn) {
};
};

const modelHandler = function (vm, model, e) {
// TODO: support test.abc & test[0]["abc"]
vm[model.expr] = e.detail.value;
}

const proxyHandler = function (e) {
let vm = this.$wepy;
let type = e.type;
Expand All @@ -32,6 +37,14 @@ const proxyHandler = function (e) {
let handlers = rel.handlers ? (rel.handlers[evtid] || {}) : {};
let fn = handlers[type];

if (rel.info.model && type === rel.info.model.type) {
modelHandler(vm, rel.info.model, e);

if (!fn) {
return;
}
}

let i = 0;
let params = [];
while (i++ < 26) {
Expand All @@ -43,7 +56,6 @@ const proxyHandler = function (e) {
params.push(dataset[key]);
}


let $event = new Event(e);

if (isFunc(fn)) {
Expand Down
2 changes: 2 additions & 0 deletions packages/wepy-cli/core/init/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ exports = module.exports = function (ins) {

'./../plugins/template/parse',
'./../plugins/template/parseClassAndStyle',
'./../plugins/template/directives/v-model',


'./../plugins/helper/supportSrc',

Expand Down
49 changes: 49 additions & 0 deletions packages/wepy-cli/core/plugins/template/directives/v-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const CONST = require('../../../util/const');

exports = module.exports = function () {

this.register('template-parse-ast-attr-v-model', function parseBindClass ({item, name, expr, modifiers, scope}) {

let attrs = item.attribs;

let conflicts = ['value', 'v-bind', ':value'].filter(v => !!attrs[v]);

conflicts.forEach(c => {
this.logger.warn('v-model', `${c}="${attrs[c]}" conflicts with v-model on element <${item.name}>`);
delete attrs[c];
});

expr = expr.trim();

return {
model: {
tag: item.name,
expr: expr
}
};
});

this.register('template-parse-ast-attr-v-model-apply', function parseBindClass ({ parsed, attrs, rel }) {

let model = parsed.model;
let expr = model.expr.trim();

if (rel.model) {
return;
}

if (model.tag === 'input') {

attrs.value = `{{ ${expr} }}`;

if (!attrs.bindinput) {
attrs.bindinput = CONST.EVENT_PROXY;
}
rel.model = {
type: 'input',
expr: expr
};
}
});
};

26 changes: 17 additions & 9 deletions packages/wepy-cli/core/plugins/template/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const parseHandler = (key = '', value = '', modifiers = {}, scope) => {
if (modifiers.capture) {
type = 'capture-';
}
type = type + (modifiers.stop ? 'catch' : 'bind') + ':' + key;
type = type + (modifiers.stop ? 'catch' : 'bind') + key;
return {
event: key,
type: type,
Expand All @@ -122,7 +122,7 @@ const parseHandler = (key = '', value = '', modifiers = {}, scope) => {
exports = module.exports = function () {


this.register('template-parse-ast-attr-v-bind', function parseAstBind (name, value, modifiers) {
this.register('template-parse-ast-attr-v-bind', function parseAstBind (item, name, value, modifiers, scope) {
return {
name: name,
prop: name.replace(bindRE, ''),
Expand All @@ -131,7 +131,7 @@ exports = module.exports = function () {
};
});

this.register('template-parse-ast-attr-v-on', function parseAstOn (evt, handler, modifiers, scope) {
this.register('template-parse-ast-attr-v-on', function parseAstOn (item, evt, handler, modifiers, scope) {
evt = evt.replace(onRE, '');
let info = parseHandler(evt, handler, modifiers, scope);
let parsed = {};
Expand Down Expand Up @@ -220,29 +220,37 @@ exports = module.exports = function () {

({ item, name, expr } = this.hookUniqueReturnArg('template-parse-ast-pre-attr-' + name, { item, name, expr }));

parsed = this.hookUnique('template-parse-ast-attr-' + name, { item, name, expr, scope });

let modifiers = parseModifiers(name);
if (modifiers) {
name = name.replace(modifierRE, '');
}
parsed = this.hookUnique('template-parse-ast-attr-' + name, { item, name, expr, modifiers, scope });

if (parsed && parsed.scope) {
scope = parsed.scope;
}

let modifiers = parseModifiers(name);
if (modifiers) {
name = name.replace(modifierRE, '');
let applyHook = `template-parse-ast-attr-${name}-apply`;
if (this.hasHook(applyHook)) {
this.hookUnique('template-parse-ast-attr-' + name + '-apply', { parsed, attrs: parsedAttr, rel });
continue;
}

let handlers = {};
let isHandler = false;

if (bindRE.test(name)) { // :prop or v-bind:prop;

let parsedBind = this.hookUnique('template-parse-ast-attr-v-bind', name, expr, modifiers, scope);
let parsedBind = this.hookUnique('template-parse-ast-attr-v-bind', item, name, expr, modifiers, scope);
if (isComponent) { // It's a prop
parsedAttr[parsedBind.prop] = parsedBind.expr;
} else {
// TODO:
}

} else if (onRE.test(name)) { // @ or v-on:
let parsedOn = this.hookUnique('template-parse-ast-attr-v-on', name, expr, modifiers, scope);
let parsedOn = this.hookUnique('template-parse-ast-attr-v-on', item, name, expr, modifiers, scope);
if (isComponent) {
rel.on[parsedOn.event] = rel.handlers.length;
rel.handlers.push({
Expand Down
4 changes: 2 additions & 2 deletions packages/wepy-cli/core/plugins/template/parseClassAndStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports = module.exports = function () {
return `${exp} ? '${name}' : ''`;
});
// return {} means remove :class
return {};
return { attrs: {} };
});


Expand All @@ -24,7 +24,7 @@ exports = module.exports = function () {
return `'${name}:' + ${exp} + ';'`;
});
// return {} means remove :class
return {};
return { attrs: {} };
});


Expand Down
3 changes: 3 additions & 0 deletions packages/wepy-cli/core/util/const.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports = module.exports = {
EVENT_PROXY: '_proxy',
};

0 comments on commit 514b1a1

Please sign in to comment.