背景

我 Build 了一个模块化的JS Core,用于pub-sub eventing,基于Nicholas Zakas的设计和Andrew Burgess的walkthrough .

我知道还有其他方法可以实现这一点,在Polymer Communications and Messaging Page中有所描述 . 但是,我不喜欢核心信号,推荐的pub-sub方法,以及正确的JS Core和Sandbox .

我的问题是当我将Polymer定义中的Method作为特定事件的回调传递时 . 当引发事件并从Core调用该方法时,“this”反映了绑定事件的列表,而不是Polymer元素中的方法和属性 . 这意味着我无法更新任何数据绑定变量以有效地更新元素的DOM .

代码

本守则已被删除,以显示反映该问题的相关方法

<polymer-element name="product-panel"> <template>

<div id="product-panel">
    <content>
    <ul>

        <template repeat="{{item in list}}">
        <li id="{{item.id}}" data-unique-keyword="{{item.keywords}}" style="{{item.styleString}}" on-click="{{addToCart}}"><img src="/ptp/resources/img/{{item.imgFile}}">
            <p>{{item.label}}</p></li>
        </template>
    </ul>
    </content>
</div>

</template> <script>
    Polymer('product-panel', {
    ready : function() {

        this.input = 'test-input';
        this.list = [{id: '1', keywords: 'red', imgFile:'1.jpg', label:'First Item', style:{width:"120px"}, styleString:''},
                     {id: '2', keywords: 'blue', imgFile:'2.jpg', label:'Second Item', style:{}, styleString:''},
                     {id: '3', keywords: 'mobile', imgFile:'3.jpg', label:'Third Item', style:{}, styleString:''}];

        this.name = 'panel';
        this.sandbox = null;

        CORE.create_module(this.name, function(sb, lns) {
        if(lns){
            lns.sandbox = sb;
        }       

        return {
            init : function() {
            },
            destroy : function() {
            }};
        }, this);

        this.sandbox.listen({'perform-search' : this.searchPlain});
        CORE.start(this.name, this);
    },
    searchPlain : function(query) {
        for (; product = this.list[i++];) {
        if (product.label.toLowerCase().indexOf(query.toLowerCase()) < 0) {
            product.style.opacity = '0.2';
        }
        }

        }
    });
    </script> </polymer-element>
    </html>

值得注意的部分是sandbox.listen方法,它将searchPlain方法绑定到perform-search事件 . 我可以确认这个方法正在被正确回调,但它无权访问数据绑定this.list,因此它无法更新值 .

如果我通过Chrome 42中的Dev Tools检查“this”,我看到它可以访问绑定事件列表及其方法 . 这让我相信这正在进行回调的核心范围 .

问题

是否期望将Polymer方法作为回调传递会导致它失去本地范围?

如果是这样,还有另一种方法将这些方法绑定到具有JS Core的事件,或者是一种解决方法,以便能够从回调方法访问本地元素变量?