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 for loop parameters in slot name #504 #510

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 34 additions & 5 deletions src/view/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,41 @@ Component.prototype._initSourceSlots = function (isFirstTime) {
if (slotBind) {
isFirstTime && this.sourceSlotNameProps.push(slotBind);

var slotName = evalExpr(slotBind.expr, this.scope, this.owner);
target = this.sourceSlots.named[slotName];
if (!target) {
target = this.sourceSlots.named[slotName] = [];
// 处理slot+for的情况:https://github.com/baidu/san/issues/504
// 因为slot的name包含插值,且插值包含迭代变量,所以需要做特殊处理
var forDirective = child.directives
&& slotBind.expr
&& (slotBind.expr.type == ExprType.TEXT || slotBind.expr.type == ExprType.INTERP)
&& child.directives['for'] ; // eslint-disable-line dot-notation
var listData = null;
if(forDirective) {
listData = evalExpr(forDirective.value, this.scope, this.owner);
}

// 插槽未被scoped
if (forDirective && listData) {
for (var index in listData) {
if (listData.hasOwnProperty(index) && listData[index] != null) {
var itemScope = {};
itemScope[forDirective.item] = listData[index];
itemScope[forDirective.index || '$index'] = index;
var itemData = new Data(itemScope, this.scope);
var slotName = evalExpr(slotBind.expr, itemData, this.owner);
target = this.sourceSlots.named[slotName] = [];
// cache item data for render
target.scope = itemData;
target.push(child.forRinsed);
}
}
}
else {
var slotName = evalExpr(slotBind.expr, this.scope, this.owner);
target = this.sourceSlots.named[slotName];
if (!target) {
target = this.sourceSlots.named[slotName] = [];
}
target.push(child);
}
target.push(child);
}
else if (isFirstTime) {
target = this.sourceSlots.noname;
Expand Down
2 changes: 1 addition & 1 deletion src/view/slot-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function SlotNode(aNode, parent, scope, owner, reverseWalker) {
// child owner & child scope
if (this.isInserted) {
this.childOwner = owner.owner;
this.childScope = owner.scope;
this.childScope = matchedSlots.scope || owner.scope;
}

if (initData) {
Expand Down
59 changes: 59 additions & 0 deletions test/slot.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2994,4 +2994,63 @@ describe("Slot", function () {
done();
});
});

it("slot prop contains for loop parameters", function (done) {
var Folder = san.defineComponent({
template: ''
+'<div>'
+ '<p s-for="i in files" class="file-{{i}}"><slot name="slot-{{i}}"/>(<slot name="slot-tip-{{i}}"/>)</p>'
+'</div>',
initData: function () {
return { files: ['error', 'rick'] }
}
});

var MyComponent = san.defineComponent({
components: {
'x-folder': Folder
},

initData: function () {
return {
files: ['error', 'rick'],
tips: {
'error': 'bi',
'rick': 'du'
}
}
},

template: ''
+ '<div>'
+ '<x-folder>'
+ '<b s-for="i, j in files" slot="slot-{{i}}">{{ j }}-{{ i }}</b>'
+ '<span s-for="i in files" slot="slot-tip-{{i}}">{{ tips[i] }}</span>'
+ '</x-folder>'
+ '</div>'
});

var myComponent = new MyComponent();
var wrap = document.createElement('div');
document.body.appendChild(wrap);
myComponent.attach(wrap);

var p0 = wrap.getElementsByTagName('p')[0];
var bs0 = p0.getElementsByTagName('b');
expect(bs0.length).toBe(1);
expect(bs0[0].innerHTML).toBe('0-error');
var span0 = p0.getElementsByTagName('span');
expect(span0.length).toBe(1);
expect(span0[0].innerHTML).toBe('bi');

var p1 = p0.nextSibling;
bs1 = p1.getElementsByTagName('b');
expect(bs1.length).toBe(1);
expect(bs1[0].innerHTML).toBe('1-rick');
var span1 = p1.getElementsByTagName('span');
expect(span1.length).toBe(1);
expect(span1[0].innerHTML).toBe('du');

done();
});
});