diff --git a/lib/base.js b/lib/base.js index feb67415..f4ac127f 100644 --- a/lib/base.js +++ b/lib/base.js @@ -66,7 +66,12 @@ define( } else { this.attr[key] = attrs[key]; } + + if (typeof this.attr[key] == 'function') { + this.attr[key] = this.attr[key].call(this); + } } + } function initDeprecatedAttributes(attrs) { diff --git a/test/spec/attribute_spec.js b/test/spec/attribute_spec.js index 6c26c2e1..b30ba8f3 100644 --- a/test/spec/attribute_spec.js +++ b/test/spec/attribute_spec.js @@ -23,6 +23,14 @@ define(['lib/component', 'lib/debug'], function (defineComponent, debug) { this.attributes({core: 1, extra: 38}); } + function testComponentWithFunctionAttribute() { + this.attributes({ + f: function() { + return this.node.nodeName.toLowerCase() == 'body'; + } + }); + } + it('adds core defaults', function () { var TestComponent = defineComponent(testComponentDefaultAttrs); var instance = (new TestComponent).initialize(document.body); @@ -95,6 +103,18 @@ define(['lib/component', 'lib/debug'], function (defineComponent, debug) { TestComponent.teardownAll(); }); + + it('will evaluate attributes that are functions at initialize time', function() { + var TestComponent = defineComponent(testComponentWithFunctionAttribute); + + var instance = (new TestComponent).initialize(document.body); + expect(instance.attr.f).toBe(true); + + var instance2 = (new TestComponent).initialize($('div').get(0)); + expect(instance2.attr.f).toBe(false); + + TestComponent.teardownAll(); + }); }); describe('(Core) this.defaultAttrs', function() {