我想在某些条件下添加聚合物元素。必须从服务器异步请求条件
request().done(function(msg){//msg is the answer from the server});
我在Polymer文档中找到了dom-if。如果我正确理解,则必须将一个属性或计算的属性传递给if条件
<template is="dom-if" if="{{...}}></template>
我不知道如何结合使用dom-if的异步方法调用
<template is="dom-if" if="{{request().done(...)}}></template>
编辑:
我想在不同页面上多次使用dom-if。所以我将其实现为具有条件功能的新行为
showElement(id: String)
<template is="dom-if" if="{{showElement(foo)}}"></template>
问题是,该服务器请求是异步的,因此我无法在行为中返回答案:
showElement: function(id) {
request(id).done(function(anwser) {
return answer;
}
}
谁能帮我?
提前致谢
最好的祝福,
基督教
最佳答案
如果处理服务器响应的方法在上下文中知道包含dom-if的元素,则可以在其中设置绑定到dom-if的属性。
您在元素中声明一个包含dom-if的属性,并将其默认设置为false。
//properties declaration...
showElement : {
type:Boolean,
value: false
}
//others properties and end of properties declaration...
您声明将dom-if绑定到此属性
<template is="dom-if" if="{{showElement}}">...</template>
在处理服务器响应的方法中,类似这样。
if(condition is met){
this.showElement = true;
}
最后一部分假设您的处理函数的上下文是包含属性和dom-if的元素之一。
如果不是这种情况,我将需要更多有关如何触发和处理请求以扩展此响应并说明如何检索它的详细信息。