Fork me on GitHub
#yada
<
2016-02-22
>
nha10:02:14

My previous problems with yada are fixed simple_smile I have a question though : is there an easy way to make a ring request from a yada ctx ? I am looking at integrating sente (https://github.com/ptaoussanis/sente) , which needs functions like ring-ajax-post ring-ajax-get-or-ws-handshake. I know Zach Tellman is working on an aleph adapter https://github.com/ztellman/sente . But I am wondering : what kind of support in yada will it require ?

martinklepsch14:02:40

So I'm having a bidi question, I hope this place is ok to ask simple_smile

martinklepsch14:02:53

I was using a routing setup like the above to do i18n routing but now I just noticed that this approach fails to match properly if the structures of routes are not unique. I.e. in the example above the bikes routes will never be matched when using (bidi/match-route routes path)

martinklepsch14:02:26

If anyone has some suggestions for some internationalized routing setup with bidi that account for this problem I'd be very happy simple_smile

martinklepsch14:02:29

I think an approach would be to generate the routing table. will try that now

martinklepsch14:02:35

hm. but with a generated routing table it becomes much harder to have something like path-for that respects language constraints

malcolmsparks19:02:52

@martinklepsch: hi! - yes, your routes won't work because bidi sees [ :i18n-cars ] as [ :i18n-bikes ] as the same thing - however, generating your routes seems likek a great idea - the reason bidi uses data for routes is to allow this approach

malcolmsparks19:02:06

I'm thinking through your remaining path-for issue -

malcolmsparks19:02:39

Could you create something like bidi.bidi/RoutesContext to add a language 'tag' to the match-context

malcolmsparks19:02:20

You could then restrict the unresolve-handler part of the Matched protocol it would satisfy to restrict on both handler and language

malcolmsparks19:02:04

the current path-for implementation looks like this:

malcolmsparks19:02:09

(defn path-for
  "Given a route definition data structure, a handler and an option map, return a
  path that would route to the handler. The map must contain the values to any
  parameters required to create the path, and extra values are silently ignored."
  [route handler & {:as params}]
  (when (nil? handler)
    (throw (ex-info "Cannot form URI from a nil handler" {})))
  (unmatch-pair route {:handler handler :params params}))

malcolmsparks19:02:23

You could create an alternative that would

malcolmsparks19:02:38

(unmatch-pair route {:handler handler :params params :lang lang})

malcolmsparks19:02:02

you would then wrap your language specific routes in a Matched record (you would have to implement)

malcolmsparks19:02:36

that Matched record would have an unresolve-handler implementation that would do sometihng similar to TaggedMatch:

malcolmsparks19:02:40

(defrecord TaggedMatch [matched tag]
  Matched
  (resolve-handler [this m]
    (resolve-handler matched (assoc m :tag tag)))
  (unresolve-handler [this m]
    (if (keyword? (:handler m))
      (when (= tag (:handler m)) "")
      (unresolve-handler matched m))))

malcolmsparks19:02:04

maybe called LanguageMatch

malcolmsparks19:02:00

So in summary, I think you need to build a couple of things: a) a defrecord language route wrapper and b) an alternative implementation of path-for, e.g. path-for-lang

malcolmsparks19:02:40

but it's definitely possible - just one of those things bidi doesn't support out of the box but clojure's record feature lets you recover from

malcolmsparks19:02:03

if any of this is unclear let me know and I'll try to explain better

martinklepsch19:02:29

@malcolmsparks: I ended up with another approach that prunes parts of the routing table:

(defn available-routes
  "Generate a routing table scoped to a given language"
  [lang]
  (->> {(name lang) {"" :root "/" (lang i18n-mapping)}}
       (assoc-in routes [1 [[keyword :country] "/"]])))

martinklepsch19:02:37

and then using that for path-for and having hard coded i18n path segments next to each other in the table I pass to match-route