2015-06-29 13 views
5

नहीं बदलता है मैं re-frame ढांचे के साथ खेल रहा हूं।पुनः फ्रेम: इनपुट: ऑन-चेंज रीसेट! इनपुट मान


नीचे कोड में, मैं इनपुट मूल्य को अद्यतन करने के साथ परेशानी हो रही हूँ जब में उपयोगकर्ता प्रकार कुछ:

(defn measurement-input [{:keys [amount unit path]}] 
    (let [amt (atom amount)] 
    (fn [] 
     [:div 
     [:input {:type "text" 
       :value @amt 
       :on-change #(reset! amt (-> % .-target .-value))}] 
     [:input {:type "button" 
       :value unit}]]))) 

इनपुट मूल्य जब तक जब तक मैं :defaultValue को :value बदल नहीं बदलेगा। मुझे पूरा यकीन है कि उपरोक्त उदाहरण Reagent के इनपुट उदाहरण को बारीकी से दर्पण करता है।


नीचे दिए गए कोड में, जब मैं इनपुट मान अद्यतन करता हूं तो मैं दो चीजें करने की कोशिश कर रहा हूं। मैं reset! इनपुट के मूल्य के साथ-साथ dispatch किसी ईवेंट हैंडलर के मान की कोशिश कर रहा हूं। मैंने इन दोनों फ़ंक्शन कॉल को do कॉल में लपेट लिया है।

यह भी ध्यान में है कि नीचे दिए गए कोड में, उपयोगकर्ता टेक्स्ट फ़ील्ड में मान अपडेट करने में सक्षम है।

(defn measurement-input [{:keys [amount unit path]}] 
    (let [amt (atom amount)] 
    (fn [] 
     [:div 
     [:input {:type "text" 
       :value @amt 
       :on-change (do #(reset! amt (-> % .-target .-value)) 
           (re-frame/dispatch [:update-value @amt]))}] 
     [:input {:type "button" 
       :value unit}]]))) 

जावास्क्रिप्ट कंसोल में, मैं निम्नलिखित त्रुटि मिलती है:

Uncaught TypeError: Cannot read property 'call' of null template.cljs?rel=1435381284083:101 

किसी भी मदद की सराहना की है हर कोई!

उत्तर

3

क्लोजरस्क्रिप्ट Google समूह में डैनियल केर्स्टन ने मुझे बताया कि कोड स्निपेट क्यों काम नहीं कर रहे थे। पोस्ट का लिंक here है।


1 कोड स्निपेट

reagent ओवरराइड clojure के उसके अपने कार्यान्वयन के साथ परमाणु। re-frame का views.cljs डिफ़ॉल्ट रूप से इसका संदर्भ नहीं देता है। एक बार जब आप reagent के atom के संस्करण का संदर्भ लेंगे, तो चीजें काम करेंगी।

views.cljs फ़ाइल के शीर्ष पर, यह परिवर्तन:

(ns performance-tracker.views 
    (:require [re-frame.core :refer [subscribe dispatch]])) 

रहे हैं:

(ns performance-tracker.views 
    (:require [reagent.core :as reagent :refer [atom]] 
       [re-frame.core :refer [subscribe dispatch]])) 

2 कोड स्निपेट

:on-change एक समारोह की उम्मीद है। मैं do ब्लॉक में गुजर रहा था। फ़ंक्शन के अंदर बस do ब्लॉक को लपेटने से त्रुटि ठीक हो गई। सही कोड के लिए नीचे देखें:

(defn measurement-input [{:keys [amount unit path]}] 
    (let [amt (atom amount)] 
    (fn [] 
     [:div 
     [:input {:type "text" 
       :value @amt 
       :on-change #(do (reset! amt (-> % .-target .-value)) ;; argument literal, %, is the event passed in the function callback 
          (re-frame/dispatch [:update-value @amt [:measurements path :amount]]))}] 
     [:input {:type "button" 
       :value unit}]]))) 
संबंधित मुद्दे