我遇到了这个问题,解决方法如下......
Javascript:为了简洁起见,全部放在一起,但很容易分开。
(function(){
var TestComponent, LoaderComponent;
TestComponent = flight.component(function() {
this.doSomething = function()
{
console.log('hi there...');
};
this.after('initialize', function() {
this.on('mouseover', this.doSomething);
});
});
LoaderComponent = flight.component(function() {
this.attachComponents = function()
{
TestComponent.attachTo('.test');
};
this.after('initialize', function() {
// Initalise existing components
this.attachComponents();
// New item created, so re-attach components
this.on('newItem:testComponent', this.attachComponents);
});
});
LoaderComponent.attachTo('body');
}());
HTML:请注意,.test
存在一个节点。这将在初始化时由 Flight 拾取(即不是动态的)。然后我们使用 jQuery 添加第二个.test
节点并触发LoaderComponent
正在侦听的事件。
<div class="test">
<p>Some sample text.</p>
</div>
<script>
$('body').append('<div class="test"><p>Some other text</p></div>').trigger('newItem:testComponent');
</script>
这显然是一个非常人为的示例,但应该表明可以将 Flight 与动态创建的元素一起使用。
希望有帮助:)