Pass polymer form data to rest api

787 views
Skip to first unread message

alberto...@gmail.com

unread,
Oct 9, 2014, 4:58:16 AM10/9/14
to polym...@googlegroups.com
I'm looking for some way to pass data from a Polymer form fields to REST API,
actually, I'm using core-ajax to do it but I think is a bit heavy method to do it.

Are any standard way to do it?

This is my code:

    <template>
   
<section>
       
<file-input class="blue" id="file" extensions='[ "xls" ]' maxFiles="1">{{ FileInputLabel }}</file-input>
   
</section>
   
<section>
       
<paper-button raised class="blue" disabled?="{{ (! Validated) || (Submitted) }}" on-tap="{{ Submit }}">
       
<core-icon icon="send"></core-icon>
                 Process
       
</paper-button>
   
</section>
   
   
<paper-toast id="toast" text=""></paper-toast>


   
<core-ajax id="ajax" url="/import-pdi" method="POST" handleAs="json" response="{{ response }}" on-core-complete="{{ SubmitFinished }}"></core-ajax>
   
</template>


       
<script>
           
Polymer("import-pdi-form", {
                   
               
Validated: false,
               
Submitted: false,
               
               
FileInputLabel: "SELECT",
                ready
: function () {
                   
this.shadowRoot.querySelector("#file").addEventListener("change", function(event) {
                       
var container = document.querySelector("import-pdi-form");
                        container
.Validated = (event.detail.valid.length != 0);
                       
if (event.detail.valid.length == 0) {
                            container
.shadowRoot.querySelector("#toast").text = "Invalid Format";
                            container
.shadowRoot.querySelector("#toast").show();
                            container
.FileInputLabel = "SELECCIONA L'ARXIU";
                       
}
                       
else {
                            container
.FileInputLabel = event.detail.valid[0].name;
                           
var form_data = new FormData();
                            form_data
.append("file", event.detail.valid[0], event.detail.valid[0].name);
                            container
.shadowRoot.querySelector("#ajax").body = form_data;
                            container
.shadowRoot.querySelector("#ajax").contentType = null;
                       
}
                   
});
               
},
               
Submit: function() {
                   
if ((this.Validated) && (! this.Submitted)) {
                       
this.Submitted = true;
                       
this.shadowRoot.querySelector("#ajax").go();
                   
}
               
},
               
SubmitFinished: function(event, detail, sender) {
                   
if (detail.xhr.status == 200) {
                       
this.shadowRoot.querySelector("#toast").text = JSON.parse(detail.xhr.response).message;
                   
}
                   
else {
                       
this.shadowRoot.querySelector("#toast").text = "Server Error";
                   
}
                   
this.shadowRoot.querySelector("#toast").show();
                   
this.FileInputLabel = "SELECCIONA L'ARXIU";
                   
this.shadowRoot.querySelector("#file").reset();
                   
this.Submitted = false;
               
}
           
});
         
</script>

Eric Bidelman

unread,
Oct 9, 2014, 12:35:58 PM10/9/14
to alberto...@gmail.com, polymer-dev

Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/1b85fd53-be7f-4159-82dd-27bddc69d865%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages