简体   繁体   English

路由参数的Polymer Ajax绑定不起作用

[英]Polymer Ajax Binding from routing parameter not working

I am using iron-pages and app-router to send me to a new page, where I need an iron-ajax element to send a request with the parameter sent from the router. 我正在使用Iron-Pages和App-Router将我发送到新页面,在这里我需要Iron-ajax元素来发送带有从路由器发送的参数的请求。

However, when I try to add my parameter {{parameter.identifier}} in iron ajax it does not work. 但是,当我尝试在Iron Ajax中添加参数{{parameter.identifier}}时,它不起作用。

I suspect it has something to do with the routing parameters being local and that iron-ajax does not see it? 我怀疑这与本地的路由参数有关,并且Iron-ajax看不到它吗? I have tried to add a property for the param, and a getter function but nothing seems to be working... 我试图为该参数添加一个属性,以及一个getter函数,但是似乎没有任何效果。

Furthermore, I know the ajax is correct since if I change the binding variable {{parameter.identifier}} to a value that exists in the database it queries alright. 此外,我知道ajax是正确的,因为如果我将绑定变量{{parameter.identifier}}更改为数据库中存在的值,它将查询正确。

<dom-module id="cst-data">
    <template>
        <style> 

    </style>

    <triplat-route name="dataRoute" params="{{parameters}}"></triplat-route>

    <iron-ajax
    id="getData"
    auto
    url="http:/.../oslc/os/OSLCPERSON?"
    params='{"oslc.where":"dcterms:identifier={{parameters.identifier}}"
            }'  

    headers='{"Content-Type": "application/json;charset=utf-8"'     
    handle-as="json"
    on-response="handleResponse"
    ></iron-ajax>
            <paper-card>{{parameters.identifier}}</paper-card>
            <paper-card>{{dataRes.name}}</paper-card>
</template>
</dom-module>
<script>
    Polymer({

        is: "cst-data" ,
        handleResponse: function () {
            this.dataRes = this.$.getData.lastResponse['rdfs:member'];
            }

    });
</script>

I found the solution, 我找到了解决方案,

What I could find was that the parameters in iron-ajax cant really handle dynamic inputs, so eventually, I got it working with a getter function. 我可以发现,iron-ajax中的参数不能真正处理动态输入,因此最终,我使它与getter函数一起使用。 Then another problem appeared in that the getter return was inputting characters and not the string to the query, by adding a new temporary string first and return the string I was able to fix it. 然后另一个问题出现了,getter返回是在输入字符而不是查询字符串时,首先添加一个新的临时字符串并返回我能够修复的字符串。

    <triplat-route id="maximoDataRoute" name="maximo-data" params="{{parameter}}"></triplat-route>

<iron-ajax
    id="ajax"
    url="http://.../"
    headers='{"Content-Type": "application/json;charset=utf-8"'     
    handle-as="json"
    params$='{{_getParams(parameter)}}'
    on-response="handleResponse"
    auto></iron-ajax>

<script>

    Polymer({

        is: "cst-employee-maximo-data" ,
        properties: {
            res: Object,
            tempString: String,
            parameter :{
                type: Object,
                notify: true
            },
        },
        handleResponse: function (event) {
            console.log("Entering handleResponse")
            this.res = event.detail.response['member'];
            console.log(this.res)   
        },

       _getParams: function(parameter) {
        this.tempString = '{"oslc.where":"dcterms:identifier='+ this.parameter.identifier+'","oslc.select":"*"}'
        console.log("tempString: "+this.tempString)
        return this.tempString   
       }

   });
</script>

Another interesting thing I noted was that in the function _getParameters, I had to input a parameter, and it did not matter what I inputted, but if it did not have it, the "parameter.identifier" was unidentified, if someone can explain this it would be greatly appreciated! 我注意到的另一件有趣的事情是,在函数_getParameters中,我必须输入一个参数,而输入的内容并不重要,但是如果没有它,则无法识别“ parameter.identifier”,如果有人可以解释这一点的话。这将不胜感激!

The way, you did it, is right. 您做到的方式是正确的。 It's normal that the function _getParams return whole query string. _getParams函数返回整个查询字符串是正常的。

Inside _getParams function, you don't need to use this.parameter.identifier but only parameter.identifier because you are passing identifier into that function as argument from binding {{_getParams(parameter)}} . _getParams函数内部,无需使用this.parameter.identifier而仅需使用parameter.identifier因为您正在将identifier作为绑定{{_getParams(parameter)}}传递给该函数。 This is great approach, because whenever this.parameter changes, function _getParams is called automatically. 这是很好的方法,因为只要this.parameter更改,函数_getParams就会自动调用。 Otherwise you would not be able to ever again change params in iron-ajax 否则,你将无法再改变过paramsiron-ajax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM