lein new hc-template cljs-management
命令行启动
lein repl
使用emacs+cider启动clj,
M-x cider-jack-in-clj
命令行启动
npm install
shadow-cljs server
使用emacs+cider启动cljs,
M-x cider-jack-in-cljs,选择shadow
:source-paths ["src/cljc" "src/cljs" "env/dev/cljs"]
:builds
{:app
{:target :browser
:output-dir "target/cljsbuild/public/dev/js"
:asset-path "/js"
:modules {:app {:entries [cljs-management.app]}}
:devtools {:watch-dir "resources/public"
:loader-mode :eval
:preloads [re-frisk.preload]
:console-support true}
:closure-defines {"re_frame.trace.trace_enabled_QMARK_" true}
:dev
{:closure-defines {cljs-management.config/domain "http://localhost:3000"
cljs-management.common.utils/print-log? true}}}}
通过修改goog-define定义参数名赋值
(goog-define domain "http://localhost:3000")
shadow-cljs.edn配置文件指定环境里app的entries的namespace为: xxx.app
(core/init! true)
(def routes
(reagent.core/atom
[["" {:name :home
:title "首页"
:icon "home"
:page index-page}]
["/index" {:name :index :title "首页管理" :icon "home"}]
["/system" {:name :system :title "系统管理" :icon "setting"}
["/user" {:name :system-user
:title "系统用户"
:page sys-user-page}]
["/menu" {:name :system-menu
:title "系统菜单"
:page sys-menu-page}]]]))
扩展
(require '[reagent.core :as r])
(def num (r/atom 1))
(swap! num inc) ; => (inc num) => num = 2
(reset! num 5) ; => num = 5
(kf/start! {:initial-db nil})
reg-event-ctx 更低级别,接收整个context,极少使用
{:coeffects {:event [:some-id :some-param]
:db <original contents of app-db>}
:effects {:db <new value for app-db>
:dispatch [:an-event-id :param1]}
:queue <a collection of further interceptors>
:stack <a collection of interceptors already walked>}
Example
(kf/reg-controller
::product-form-controller
{:params (fn [route]
(let [path (get-in route [:path-params :path])
query (get route :query-string)]
(when (and (= path "/index/qianggou-time/product-detail")
(re-find #"id=\w+" (or query "")))
(second (string/split query "=")))))
:start (fn [_ id]
(rf/dispatch [::fetch-time-product-detail id]))})
启动时候仅仅执行一次的操作
{:params (constantly true) ;; true, or whatever non-nil value you prefer
:start [:call-me-once-then-never-again]}
每次发生路由的时候都做一次的操作,比如logging
{:params identity
:start [:log-user-activity]}
path-for
(kee-frame.core/path-for [:todos {:id 14}]) => "/todos/14"
navigate-to
(reg-event-fx
:todo-added
(fn [_ [todo]]
{:db (update db :todos conj todo)
:navigate-to [:todo :id (:id todo)]]}) ;; "/todos/14"
(require '[day8.re-frame.http-fx]) (require '[ajax.core :as ajax])
json请求
(re-frame/reg-event-fx
::http-post
(fn [{:keys [db]} _]
{:db (assoc db :show-loading true)
:http-xhrio {:method :post
:uri "https://httpbin.org/post"
:params data
:timeout 5000
:format (ajax/json-request-format)
:response-format (ajax/json-response-format {:keywords? true})
:on-success [::success-post-result]
:on-failure [::failure-post-result]}}))
form请求
(re-frame/reg-event-fx
::http-form
(fn [_world [_ val]]
{:http-xhrio {:method :post
:uri "https://localhost:3000"
:body form-data
:headers {:authorization (get-token)}
:timeout 30000
:response-format (http/json-response-format {:keywords? true})
:on-failure [::error request-event nil]}}))
(require '[clj-http.client :as http])
(http/get "http://cdn.imgs.3vyd.com/xh/admin/test.json" {:as :json})
(http/get "http://cdn.imgs.3vyd.com/xh/admin/test.json" {:as :json})
(http/post "http://localhost:8185/management/public/userList"
{:form-params
{:mobile "15092107090"
:nickName "marvin.ma"
:name "marvin"
:openid "8"}
:content-type :json})
(http/post "http://localhost:8185/management/oauth2/token"
{:form-params
{:username "test"
:password "test123"
:client_id "management-Client"
:grant_type "password"}})
example
(kee-frame.core/reg-chain
:league/load
(fn [ctx [id]]
{:http-xhrio {:method :get
:uri (str "/leagues/" id)}})
(fn [{:keys [db]} [_ league-data]]
{:db (assoc db :league league-data)
:http-xhrio {:method :get
:uri (str "/leagues/" id)}})
(fn [{:keys [db]} [_ league-data data2]]
{:db (assoc db :league league-data)}))