2013-01-09 5 views
2

저는 Clojurescript에 비교적 익숙하며 문제가 무엇인지 잘 모르겠습니다. 필자는 clojurescript에 스크립트가로드되지 않는 FB API를로드하기 위해 문서에 새로운 스크립트 요소를 추가하는 함수를 가지고 있습니다. 해당 자원에 대한 활동이 없습니다. 나는 자바 스크립트에서 예제를 다시 작성하고 작동한다. 생성 된 clojurescript 코드를 살펴 봤는데 기본적으로 내가 쓴 JavaScript 코드와 같습니다. 몇 가지 시도를했지만 결국에는 브라우저가 ClojureScript를 사용하여 동적 스크립트를로드하지 못합니다.clojurescript 동적 스크립트 로딩

내 코드 (위의 내 cljs에서 업데이트 이전)

(ns wearthisorthat.client.fblogin 
    (:require [goog.net.XhrIo :as xhr] 
      [cljs.reader :as cljrdr] 
      [clojure.browser.repl :as repl] 
      [domina.events :as ev] 
      [domina :as d] 
      [domina.css :as css])) 

(defn load-fb-sdk [debug?] 
    (let [ id "facebook-jssdk" 
     debug-str (if debug? "/debug" "") 
     ref (d/by-id "fb-script") 
     _ (.log js/console "ref = " ref) 
     parent (.-parent ref) 
     el-id (d/by-id id) 
     element (d/string-to-dom (str "<script id=" id " async" 
             " src=//connect.facebook.net/en_US/all" 
             debug-str ".js></script>")) 
     _ (.log js/console element)] 
    (when-not el-id (d/insert-before! ref element)))) 

(defn ^:export fbcb [] 
    (let [data {:appId "<myappid>", 
       :channelUrl "<my-channel>", 
       :status true, 
       :cookie true, 
       :xfbml true}] 
    (.log js/console "RRRRRRRRRRR") 
    (js/FB.init (clj->js data)))) 

;; Load the SDK's source Asynchronously 
(.log js/console "RIGHT HERE") 
(aset js/window "fbAsyncInit" fbcb) 

(load-fb-sdk true) 

내 index.html을가 ... 내 cljs 후

<!DOCTYPE html> 
<html> 
<head> 
</head> 
<body> 
    <div id="fb-root"></div> 
    <script id="fb-script"></script> 
    <script type="text/javascript" src="js/wtot.js"></script> 
</body> 
</html> 

내 문서는 다음과 같습니다 실행됩니다

<!DOCTYPE html> 
<!-- saved from url=(0037)http://localhost:3000/wtot/index.html --> 
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
</head> 
<body> 
    <div id="fb-root"></div> 
    <script id="facebook-jssdk" async src="//connect.facebook.net/en_US/all/debug.js"></script> 
    <script id="fb-script"></script> 
    <script type="text/javascript" src="./index_files/wtot.js"></script> 

</body> 
</html> 

Google 네트워크 트래픽은 index.html과 wtot.js 만 표시하며 debug.js는 표시하지 않습니다 (오류는 없지만 n debug.js를로드하고로드하기위한 로그의 참조). 위의 동적 요소를 복사하여 내 index.html에 붙여 넣어 동적 스크립트 요소를 정적으로 만들면 코드가 예상대로 작동합니다. 앞서 언급했듯이 스크립트 요소의 동적 추가를 비롯하여이 모든 것을 자바 스크립트로 만들면 모든 것이 작동합니다. 내가 뭘 놓치고 있니?

답변

3

그냥 아이디어 : 동적으로 스크립트를 추가에 대한 couplepages를 본 적이 그들은 모두 새로운 스크립트를 작성하고 <HEAD>의 자식이 아닌 <BODY>로 추가 :

var head= document.getElementsByTagName('head')[0]; 
    var script= document.createElement('script'); 
    script.type= 'text/javascript'; 
    script.src= 'helper.js'; 
    head.appendChild(script);