简体   繁体   English

关于Polymer 1.0,使用iron-ajax制作数据服务器的问题

[英]Question about Polymer 1.0, using iron-ajax to make a data server

I have a project using Polymer 1.2, and am trying to create a data server component using iron-ajax called review_data_service.html , which can make AJAX call to send (GET,POST,PUT or DELETE) request to backend like this: 我有一个使用Polymer 1.2的项目,并且正在尝试使用Iron-ajax创建一个名为review_data_service.html的数据服务器组件,该组件可以使AJAX调用将发送(GET,POST,PUT或DELETE)请求发送到后端,如下所示:

<dom-module id="review-data-service">
        <template>
                <iron-ajax url= "[[URI]]"
                id="GET_REVIEWS"
                handle-as="json" 
                loading="{{loading}}" 
                on-response="handleResponse"
                content-type="application/json"
                auto></iron-ajax>
        </template>
        <script>
            Polymer({
                is: 'review-data-service',
                properties: {
                    reviewId : Number,
                    API: {
                        type: Object,
                        value:  () => {
                            return {
                                BASE : 'http://localhost:3000',
                                RESOURCE : 'reviews'
                            };
                        }
                    },
                    URI : {
                        type : String,
                        computed : 'setURI(API.BASE, API.RESOURCE)'
                    }
                },
                setURI : (BASE, RESOURCE) => {
                    return `${BASE}/${RESOURCE}`;
                },
                handleResponse : function (event) {
                    let promise = new Promise ((resolve, reject) => {
                        if (event.detail.response != null) {
                            resolve(event.detail.response);
                        } else {
                            reject('error');
                        }
                    });
                    promise.then(result => {
                        this.fire('getReviews', 
                            {reviews : result}
                        )
                    }, result => {
                        console.log('reject', result);
                    })
                }
            });
        </script>
    </dom-module>

In my another component, my-project-reviewer.html , I can get the review data successfully like this: 在另一个组件my-project-reviewer.html中 ,我可以像这样成功获取评论数据:

 listeners: {
                    'getReviews': '_getReviews',
            },

_getReviews: (event) => {
                this.reviews = event.detail.reviews;
                // can get the data successfully at here:
                console.log(this.reviews);
            },

However, when I try to show those data in the dom, it won't work: 但是,当我尝试在dom中显示这些数据时,它将无法正常工作:

<!-- cannot see anything here:  -->
                <template is="dom-repeat" items="[[reviews]]">
                        <div class="project-name">[[item.name]]</div>
                </template>

Does any one know what happens? 有人知道会发生什么吗? In the beginning, I didn't use Promise, I thought it maybe because of asyc issues (when dom is rendered, still cannot get data to display), so I used Promise, but still got the same result (only can get data in console) Plus, is any better way (or example) to create a data service in Polymer 1? 一开始,我没有使用Promise,我认为可能是因为asyc问题(呈现dom时,仍然无法显示数据),所以我使用了Promise,但仍然得到了相同的结果(只能从中获取数据)。控制台),还有更好的方法(或示例)在Polymer 1中创建数据服务吗? (I used to use Angular which can perfectly make this happen) Just want to create a component like which can support basic http requests (GET,POST,PUT,DELETE) to be used in another component? (我曾经使用Angular可以完美地实现这一目标)只是想创建一个可以支持在其他组件中使用的基本http请求(GET,POST,PUT,DELETE)的组件? or make the data service as a behaviors (I tried but doesn't work very well) thank you so much in advanced! 还是将数据服务作为一种行为(我曾尝试过,但效果不佳),非常感谢您!

Polymer needs to know that there is smth to do try calling Polymer需要知道有很多东西可以尝试调用

_getReviews: (event) => {
            this.set('reviews', event.detail.reviews);
            // can get the data successfully at here:
            console.log(this.reviews);
        }

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

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