2016-06-15 6 views
0

탈퇴 단계에서 새로운 받침대 인터셉터를 실행하고 싶습니다. 컨텍스트를 수정하여 각 html 페이지의베이스에 토큰 문자열을 추가하고 싶습니다 ('사이트 활성'보고에 사용). 단상 소스 코드 here에서 'after'함수를 사용하여 받침대에 인터셉터를 만드는 방법

나는이 기능을 참조하십시오
(defn after 
"Return an interceptor which calls `f` on context during the leave 
stage." 
([f] (interceptor {:leave f})) 
([f & args] 
    (let [[n f args] (if (fn? f) 
        [nil f args] 
        [f (first args) (rest args)])] 
     (interceptor {:name (interceptor-name n) 
       :leave #(apply f % args)})))) 

그래서 나는 다음 인터셉터 맵에 삽입하는 기능을 제공해야합니다. 그건 의미가 있습니다. 그러나 'context'가 범위에 포함되지 않은 경우이 함수를 작성하여 컨텍스트를 참조하는 방법은 무엇입니까?

...[io.pedestal.interceptor.helpers :as h]... 

(defn my-token-interceptor [] 
    (h/after 
    (fn [ctx] 
     (assoc ctx :response {...})))) 

그러나 'CTX'범위에되지 않습니다 :

나는 그런 짓을 할? 감사.

+0

당신은 당신의 경로에 인터셉터를 "설치"해야합니다. 코드의 일부분을 보여줄 수 있습니까? – ClojureMostly

답변

1

after 의사는 이에 대해 분명합니다.

(defn after 
"Return an interceptor which calls `f` on context during the leave 
stage." 

당신의 f의 첫 번째 인수로 context을 받게됩니다. f의 첫 번째 인수를 사용하여 f 안에 context에 액세스 할 수 있습니다. 들어 token-function

...[io.pedestal.interceptor.helpers :as h]... 

(defn token-function 
    "" 
    [ctx] 
    (assoc ctx :response {})) 

(def my-token-interceptor (h/after token-function)) 

;; inside above token-function, ctx is pedestal `context` 
1

h/after를 호출하여 h/after에 공급 h/after 반환 인터셉터 때문에, 나는 '내-토큰 인터셉터를'생성됩니다 token-function, 아래

f 기능의 샘플입니다 가치가있는 것이므로 더 이상 beforeafter 함수가이 작업을 수행하는 가장 좋은 방법이라고 생각하지 않습니다. ( io.pedestal.interceptor.helpers의 모든 기능은 지금 가지 필요하지 않습니다.)

우리의 추천, 그냥 Clojure의 맵 리터럴로 인터셉터를 작성할과 같이하는 것입니다

(def my-token-interceptor 
    {:name ::my-token-interceptor 
    :leave (fn [context] (assoc context :response {,,,}))}) 

당신은 after 기능을 추가하지 않는 것을 알 수 있습니다 명확성이나 설명 가치 측면에서의 모든 것.

은 물론 당신이 아니라 바로 거기에 익명 함수를 만드는 것보다지도의 함수 값을 사용할 수 있습니다

(defn- token-function 
    [context] 
    (assoc context :response {,,,})) 

(def my-token-interceptor 
    {:name ::my-token-interceptor 
    :leave token-function)})