Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何为聚合元素添加函数属性_Javascript_Polymer - Fatal编程技术网

Javascript 如何为聚合元素添加函数属性

Javascript 如何为聚合元素添加函数属性,javascript,polymer,Javascript,Polymer,我想创建一个具有function属性的聚合元素,在获得成功响应时将调用它 <foo-bar url="/api/getdata" succCallback="func"></foo-bar> func function(){ alert('hi'); } func函数(){ 警报(“hi”); } 我试过这个: <polymer-element name="foo-bar" attributes="url succCallback"> <te

我想创建一个具有function属性的聚合元素,在获得成功响应时将调用它

<foo-bar url="/api/getdata" succCallback="func"></foo-bar>

func function(){
  alert('hi');
}

func函数(){
警报(“hi”);
}
我试过这个:

<polymer-element name="foo-bar" attributes="url succCallback">
<template>
      <core-ajax id="ajax" method="POST" 
      url="{{url}}" 
      contentType="application/json"
      handleAs="json" 
      on-core-response="{{handleResponse}}"></core-ajax>
</template>
<script>
    Polymer({
        handleResponse: function(e){
             var response = e.detail.response;
             if (response.status === 'success') {
                 // call succCallback
                 this.fire(succCallback);
             }
         }
    });
</script>
</polymer-element>

聚合物({
HandlerResponse:函数(e){
var响应=e.detail.response;
如果(response.status==“success”){
//呼叫成功呼叫
这个。火(呼救);
}
}
});

它不起作用。如何调用此回调函数?谢谢

尝试替换此.fire(回调)this.fire(succallback())进行编码>操作

我想不行,因为

属性
attr只使用字符串

可行的解决方案可能是:

<foo-bar url="/api/getdata" succEventTrigger="foo-bar-done"></foo-bar>
和聚合物:

<polymer-element name="foo-bar" attributes="url succEventTrigger">
<template>
      <core-ajax id="ajax" method="POST" 
      url="{{url}}" 
      contentType="application/json"
      handleAs="json" 
      on-core-response="{{handleResponse}}"></core-ajax>
</template>
<script>
    Polymer({
        succEventTrigger : '',
        handleResponse: function(e){
             var response = e.detail.response;
             if (response.status === 'success') {
                 // trigger callback event with parameter if needed
                 this.fire(succEventTrigger, { param : value });
             }
         }
    });
</script>
</polymer-element>

聚合物({
成功事件触发器:“”,
HandlerResponse:函数(e){
var响应=e.detail.response;
如果(response.status==“success”){
//如果需要,使用参数触发回调事件
this.fire(succEventTrigger,{param:value});
}
}
});

编辑:我意识到我的最初回复错过了实际的失败点,晚了几分钟。聚合物元素定义很好,但它的使用需要包装在模板中,以便您可以执行以下操作:

<body>
<template is="auto-binding">
    <foo-bar url="/api/getdata" succCallback="{{func}}"></foo-bar>
</template>
<script>
  var scope = document.querySelector('template[is=auto-binding]');

  scope.func = function (whatever) {
    console.log('yay!');
  };
</script>
</body>

var scope=document.querySelector('template[is=auto binding]');
scope.func=函数(无论什么){
console.log('yay!');
};
下面的原始答复可能仍然有用,特别是在使用回调的情况下

使用“发布”块而不是属性。。。呃,属性(我现在意识到这不是错误的原因):


聚合物({
出版:{
url:未定义,
成功回调:未定义,
},
就绪:函数(){
this.url=this.url | |“一些默认值”;
this.succCallback=this.succCallback | |函数(){};
},
HandlerResponse:函数(e){
var响应=e.detail.response;
如果(response.status==“success”){
//呼叫成功呼叫
这个.succallback();
}
}
});

我实际上是在寻找“这是一种在聚合物中具有牵引力的模式,还是不受欢迎的模式?”的答案。由于回调的使用在我很好奇的文章中甚至没有提到。

在创建自定义聚合元素的实例时,需要将参数括号添加到作为属性传递的函数名中

i、 e.而不是:

<foo-bar url="/api/getdata" succCallback="func"></foo-bar>

做:


根据久经考验的事实(但有些过时):



它也不起作用。出现以下错误:未捕获类型错误:字符串不是函数谢谢!它起作用了!我发现,如果我像这样调用succCallback
windows[succCallback](),但我不知道使用它是否有任何副作用。没错,它也可以工作,但我特别避免了这种方法,因为它涉及到与窗口对象的交互。。默认情况下不好:)
    <polymer-element name="foo-bar">
<template>
      <core-ajax id="ajax" method="POST" 
      url="{{url}}" 
      contentType="application/json"
      handleAs="json" 
      on-core-response="{{handleResponse}}"></core-ajax>
</template>
<script>
    Polymer({
        publish: {
            url: undefined,
            succCallback: undefined,
        },
        ready: function(){
            this.url = this.url || "some default";
            this.succCallback = this.succCallback || function(){};
        },
        handleResponse: function(e){
             var response = e.detail.response;
             if (response.status === 'success') {
                 // call succCallback
                 this.succCallback();
             }
         }
    });
</script>
</polymer-element>
<foo-bar url="/api/getdata" succCallback="func"></foo-bar>
<foo-bar url="/api/getdata" succCallback="func()"></foo-bar>
<body onload="handleLoad()"></body>