2014-10-09 24 views
1

我正在尋找一些方法來將數據從聚合物表單域傳遞到REST API,實際上,我正在使用core-ajax來做到這一點,但我認爲是一個有點沉重的方法去做吧。通過聚合物形式的數據來休息api

是否有任何標準的方式來做到這一點?

這是我的代碼:

<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> 
+0

我在很多地方注意到你正在使用this.shadowRoot.querySelector來獲取有關id的東西。您可以使用自動節點查找來更高效地執行此操作 https://www.polymer-project.org/docs/polymer/polymer.html#automatic-node-finding – robdodson 2014-10-09 18:04:32

+0

@robdodson:感謝您的建議。我剛剛更換了shadowRoot,並且都運行良好。除此之外,我正在尋找將表單數據傳遞到服務器的最佳方式。現在我正在使用core-ajax,但它似乎有點沉重的解決方案。有關於它的標準嗎? – 2014-10-10 09:08:38

回答

3

提交包含我們目前建議您使用ajax-form element自定義元素的形式。看起來您可能已經在使用同一作者的file-input元素,所以這兩者應該很好地協同工作。

+0

謝謝!它工作得很好! – 2014-10-15 07:01:16

+0

@AlbertHornos如果我是對的,對於核心輸入和紙張輸入的最新更新使得在不使用ajax形式的情況下提交表單,但如果我理解正確,仍然可以通過傳統方式保留材料設計和發佈的風格。查看更改日誌:https://blog.polymer-project.org/releases/2014/11/12/release-0.5.1/#0-5-1_coreinput – Rexford 2014-11-20 11:22:47

+1

@Rexford這看起來不正確。 「元素不會在

元素內提交 - 最佳做法是使用以更現代的XHR方法提交數據,例如使用」「元素。該聲明也適用於其他自定義元素表單字段。 – 2014-11-21 18:04:22