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

feat: scoped include #226

Open
wants to merge 11 commits into
base: next
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.idea*
example
npm-debug.log
yarn-error.log
*/tmp/*
test/tmp/*
test/runner/dom*
Expand Down
1 change: 0 additions & 1 deletion dist/regular.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ gulp.task('v', function(fn){


gulp.task('watch', ["build", 'testbundle'], function(){
gulp.watch(['test/spec/*.js', 'lib/**/*.js'], ['jshint','testbundle'])
gulp.watch(['test/spec/*.js', 'lib/**/*.js'], ['jshint', 'build', 'testbundle'])
})


Expand Down
2 changes: 1 addition & 1 deletion lib/parser/Lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var createFSM = require("./FSM");
var conflictTag = {"}": "{", "]": "["}, map1, map2;
// some macro for lexer
var macro = {
'NAME': /(?:[:_A-Za-z][-\.:_0-9A-Za-z]*)/,
'NAME': /(?:[:_A-Za-z\$][-\.:_0-9A-Za-z]*)/,
'IDENT': /[\$_A-Za-z][_0-9A-Za-z\$]*/,
'SPACE': /[\r\n\t\f ]/
}
Expand Down
10 changes: 8 additions & 2 deletions lib/parser/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,15 @@ op.interplation = function(){

// {{~}}
op.inc = op.include = function(){
var content = this.expression();
var content, locals;

content = this.expression();

if (this.eat('IDENT', 'with')) {
locals = this.expression();
}
this.match('END');
return node.template(content);
return node.template(content, locals);
}

// {{#if}}
Expand Down
5 changes: 3 additions & 2 deletions lib/parser/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ module.exports = {
text: text
}
},
template: function(template){
template: function(template, locals){
return {
type: 'template',
content: template
content: template,
locals: locals
}
}
}
1 change: 0 additions & 1 deletion lib/render/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ var Regular = function(definition, options){


// modify在compile之后调用, 这样就无需处理SSR相关逻辑

if( oldModify ){
oldModify(this);
}
Expand Down
10 changes: 7 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,15 +476,19 @@ _.isGroup = function(group){
return group.inject || group.$inject;
}

_.blankReg = /^\s*$/;
_.blankReg = /^\s*$/;

_.getCompileFn = function(source, ctx, options){
return function( passedOptions ){
return function( passedOptions, transform ){
if( passedOptions && options ) _.extend( passedOptions , options );
else passedOptions = options;

if(typeof transform === 'function') {
passedOptions = transform(passedOptions);
}

return ctx.$compile(source, passedOptions )
}
return ctx.$compile.bind(ctx,source, options)
}

// remove directive param from AST
Expand Down
101 changes: 78 additions & 23 deletions lib/walkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,36 +296,86 @@ walkers.list = function(ast, options){

// {#include } or {#inc template}
walkers.template = function(ast, options){
var content = ast.content, compiled;
var content = ast.content, locals = ast.locals, compiled;
var placeholder = document.createComment('inlcude');
var compiled, namespace = options.namespace, extra = options.extra;
var group = new Group([placeholder]);
var cursor = options.cursor;

var localsFn, scopeName = this.$scope || '$scope';

insertPlaceHolder(placeholder, cursor);

if(content){
var self = this;
this.$watch(content, function(value){
var removed = group.get(1), type= typeof value;
if( removed){
removed.destroy(true);
group.children.pop();
}
if(!value) return;

group.push( compiled = type === 'function' ? value(cursor? {cursor: cursor}: null): self.$compile( type !== 'object'? String(value): value, {
if(locals){
localsFn = this._touchExpr(locals).get.bind(this, this, extra);
}

this.$watch(content, update, OPTIONS.INIT);

cursor = null;
}

// attach $scope to extra
function addScope(options) {
var opts = {};

_.extend(opts, options);
opts.extra = _.createObject(options.extra);

if (locals) {
opts.extra[ scopeName ] = localsFn();
self.$watch(localsFn, function (n, o) {
opts.extra[ scopeName ] = n;
}, {
deep: true,
sync: true
});
} else {
opts.extra[ scopeName ] = {};
}

return opts;
}

function hideScope() {
var opts = {};

_.extend(opts, options);
opts.extra = _.createObject(options.extra);
opts.extra[ scopeName ] = {};

return opts;
}

function update( value ){
var removed = group.get(1), type= typeof value;
if( removed){
removed.destroy(true);
group.children.pop();
}
if(!value) return;

var transform = typeof value !== 'string' ?
addScope :
hideScope

compiled = type === 'function' ?
value(cursor? {cursor: cursor}: null, transform) :
self.$compile( type !== 'object' ? String(value): value, transform({
record: true,
outer: options.outer,
namespace: namespace,
cursor: cursor,
extra: extra}) );
if(placeholder.parentNode && !cursor) {
compiled.$inject(placeholder, 'before')
}
}, OPTIONS.INIT);
cursor = null;
extra: extra
}));
group.push(compiled);
if(placeholder.parentNode && !cursor) {
compiled.$inject(placeholder, 'before')
}
}

return group;
};

Expand Down Expand Up @@ -448,7 +498,7 @@ walkers.expression = function(ast, options){
}else{
node = document.createTextNode("");
}

this.$watch(ast, function(newval){
dom.text(node, _.toText(newval));
}, OPTIONS.STABLE_INIT )
Expand Down Expand Up @@ -600,12 +650,18 @@ walkers.component = function(ast, options){
extra = options.extra,
namespace = options.namespace,
refDirective = walkers.Regular.directive('ref'),
ref, self = this, is;

ref, self = this, is, $scope;
var data = {}, events;

for(var i = 0, len = attrs.length; i < len; i++){
var attr = attrs[i];

if (attr.name === '$scope') {
$scope = attr.value;
continue;
}

// consider disabled equlasto disabled={true}

shared.prepareAttr( attr, attr.name === 'ref' && refDirective );
Expand Down Expand Up @@ -640,7 +696,7 @@ walkers.component = function(ast, options){
namespace: namespace,
extra: extra,
outer: options.outer
})
})
}

// @if is r-component . we need to find the target Component
Expand Down Expand Up @@ -684,6 +740,7 @@ walkers.component = function(ast, options){
$parent: (isolate & 2)? null: this,
$root: this.$root,
$outer: options.outer,
$scope: $scope,
_body: {
ctx: this,
ast: ast.children
Expand All @@ -695,10 +752,8 @@ walkers.component = function(ast, options){
extra: options.extra
}


var component = new Component(definition, options), reflink;


if(ref && this.$refs){
reflink = refDirective.link;
var refDestroy = reflink.call(this, component, ref);
Expand Down
Loading