Skip to content
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
6 changes: 3 additions & 3 deletions src/view/is-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function IsNode(aNode, parent, scope, owner, hydrateWalker) {
this.tagName = this.aNode.tagName;
// #[begin] hydrate
if (hydrateWalker) {
this.cmpt = evalExpr(this.aNode.directives.is.value, this.scope) || this.tagName;
this.cmpt = evalExpr(this.aNode.directives.is.value, this.scope, this.owner) || this.tagName;
this.children[0] = createHydrateNode(
this.aNode.isRinsed,
this,
Expand All @@ -63,7 +63,7 @@ IsNode.prototype.dispose = nodeOwnSimpleDispose;
* @param {HTMLElement=} beforeEl 要添加到哪个元素之前
*/
IsNode.prototype.attach = function (parentEl, beforeEl) {
this.cmpt = evalExpr(this.aNode.directives.is.value, this.scope) || this.tagName;
this.cmpt = evalExpr(this.aNode.directives.is.value, this.scope, this.owner) || this.tagName;

var child = createNode(this.aNode.isRinsed, this, this.scope, this.owner, this.cmpt);
this.children[0] = child;
Expand All @@ -77,7 +77,7 @@ IsNode.prototype.attach = function (parentEl, beforeEl) {
*/
IsNode.prototype._update = function (changes) {
var child = this.children[0];
var cmpt = evalExpr(this.aNode.directives.is.value, this.scope) || this.tagName;
var cmpt = evalExpr(this.aNode.directives.is.value, this.scope, this.owner) || this.tagName;

if (cmpt === this.cmpt) {
child._update(changes);
Expand Down
93 changes: 93 additions & 0 deletions test/component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,99 @@ describe("Component", function () {
});
});

it("s-is use component method", function () {
var Label = san.defineComponent({
template: '<span title="{{text}}">{{text}}</span>',
initData: function () {
return {
text: 'erik'
}
}
});

var MyComponent = san.defineComponent({
components: {
'x-label': Label
},
initData: function () {
return {
type: 'label'
}
},
getComponentName: function (type) {
return 'x-' + type;
},
template: '<div><test s-is="{{getComponentName(type)}}"/></div>'
});

var myComponent = new MyComponent();

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

var span = wrap.getElementsByTagName('span')[0];
expect(span.title).toBe('erik');

myComponent.dispose();
document.body.removeChild(wrap);
});

it("s-is use component method update", function (done) {
var Label = san.defineComponent({
template: '<span title="{{text}}">{{text}}</span>',
initData: function () {
return {
text: 'erik'
}
}
});

var H2 = san.defineComponent({
template: '<h2>{{text}}.baidu</h2>',
initData: function () {
return {
text: 'erik'
}
}
});

var MyComponent = san.defineComponent({
components: {
'x-label': Label,
'x-h2': H2
},
initData: function () {
return {
type: 'label'
}
},
getComponentName: function () {
return 'x-' + this.data.get('type');
},
template: '<div><test s-is="{{getComponentName()}}"/></div>'
});

var myComponent = new MyComponent();

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

var span = wrap.getElementsByTagName('span')[0];
expect(span.title).toBe('erik');

myComponent.data.set('type', 'h2');
san.nextTick(function () {
var h2 = wrap.getElementsByTagName('h2')[0];
expect(h2.innerHTML).toBe('erik.baidu');

myComponent.dispose();
document.body.removeChild(wrap);
done();
});
});

it("components in inherits structure", function () {
var Span = san.defineComponent({});
Span.template = '<span title="span">span</span>';
Expand Down