Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojurescript this-as macro points to global object

During writting reactjs tutorial in clojurescrtipt i've found that this-as macro compiles to

(function(){var t = this; return t;}

which always points to window inside react classes. Sometimes i can workaround this by js* this but not inside let or map cuz they are also compiled to functions.

How can i access react js this inside let form?

Situation on fiddle: http://jsfiddle.net/VkebS/57/

and a piece of tutorial FYI:

(def comment-list 
  (React/createClass 
   #js{:render 
       (fn [] (dom/div #js {:className "commentList"}
                       (let [d (this-as t (.. t -props -data))]
                         (map #(commnt #js {:author (:author %)} (:text %)) d))))}))

PS: i can use native array for data and native map function

(def comment-list 
  (React/createClass 
   #js{:render 
       (fn [] (dom/div #js {:className "commentList"}
                       (.map (.. (js* "this") -props -data) #(commnt #js {:author (:author %)} (:text %)))))}))

that works, but...

like image 786
snoopnstalk Avatar asked Oct 02 '22 03:10

snoopnstalk


1 Answers

this-as works if you use it at start of render function:

(def commnt
  (React/createClass
   #js {:render
        (fn []
          (this-as this
                   (dom/div #js {:className "comment"}
                            (dom/h2 #js {:className "commentAuthor"}
                                    (.. this -props -author))
                            (dom/span #js {:dangerouslySetInnerHTML
                                           #js{:__html
                                               (.makeHtml converter (..  (js* "this") -props -children toString))}}))))}))

see also: https://github.com/swannodette/om/blob/master/src/om/dom.cljs#L34

like image 93
edbond Avatar answered Oct 12 '22 12:10

edbond