MithrilJS / mithril-node-render

Use mithril views to render server side
MIT License
209 stars 44 forks source link

Missing m.route.param on the server ... #67

Closed ChrisGitIt closed 7 years ago

ChrisGitIt commented 7 years ago

Hi,

i'm currently trying your absolut masterpiece of software, but some how i'm a litte stuck ...

Within a component, i would like to use the m.route.param function, but on the server its not provided ... probably because there is no "mithril m.route()" call. Do you see a simple solution to bring this feature to your software? I'm not sure if its even possible because it looks like its more a mithril internal call ... maybe you see a workaround? Maybe a browserlyfied helper function?

Any ideas are greatly appreciate!

Greets,

Chris

StephanHoyer commented 7 years ago

Have you looked at the isomorphic example. I think there is an example how to use route params

Am 14.05.2017 19:33 schrieb "ChrisGitIt" notifications@github.com:

Hi,

i'm currently trying your absolut masterpiece of software, but some how i'm a litte stuck ...

Within a component, i would like to use the m.route.param function, but on the server its not provided ... probably because there is no "mithril m.route()" call. Do you see a simple solution to bring this feature to your software? I'm not sure if its even possible because it looks like its more a mithril internal call ... maybe you see a workaround? Maybe a browserlyfied helper function?

Any ideas are greatly appreciate!

Greets,

Chris

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/StephanHoyer/mithril-node-render/issues/67, or mute the thread https://github.com/notifications/unsubscribe-auth/AADVrf7qhykASneAEI3iEBh6zDY8fBBCks5r5zrfgaJpZM4NacUY .

ChrisGitIt commented 7 years ago

Hi again and thanks for your feedback!

You mean using the vnode.attrs? I have seen this, but i meant using it within an included component.

I think its feasible to pass all components the attributes, but mostly this will result in unwanted results (like overriding attrs or have to join them).

So to make a more obvious example:

function getDynamicHref(vnode) {
 if(m.route.param('language')) {
  //... replace vnode.attrs.href ":lang" with m.route.param('language')
 }
}

resolved_link = {
 view: function() {
  return m('a', getDynamicHref(vnode))
 }
}

component = {
 view: function(vnode) {
  return [m('h1','Dynamic link'), m(resolved_link, {'href':'/:lang/somethin'})
 }
}

I know, this might be very specific but i'm not sure if there will be other cases where this might be usefull...

Thanks for any further suggestions!

StephanHoyer commented 7 years ago

This will be hard to solve, since node is single threaded and async. So there can't be global state that is different between requests.

Only solution that comes into my mind is to do some kind of placeholder-magic.

component = {
  view: function(vnode) {
    return [
      m('h1','Dynamic link'),
      m('a', {
        href: '/{{lang}}/something'
      }, 'to something')
    ]
  }
}

and replace {{lang}} in before sending the response.

ChrisGitIt commented 7 years ago

Hi StephanHoyer and thanks again for your feedback!

That is nearly exactly what i wanted to, some placeholder magic, using the mithril route schema like /my-route/:lang/:id ... BUT on a sub component level (like mentioned, an extra "anchor" sub component that would handle the links without passing any argument/attrs by using the m.route.param from mithril.

I think i will have to resolve the issue on an APP Level, so my app provides the needed vars and my subcomponent includes the app object to take advantage of the vars.

Greetings,

Chris