tobymao / snabberb

A simple component view framework for Ruby Opal based on Snabbdom
MIT License
44 stars 2 forks source link

Get following error when use with opal, opal-opal-sprockets. #7

Closed zw963 closed 3 years ago

zw963 commented 3 years ago

Hi, i am a newbie to Opal.

I am developing a Roda web app, and Opal seem like works!

because you can see a hello output on browser console (i output it from opal)

But, when i try to following inline example in readme, get following error.

image

Could you please help on this? thank you.

Following is my source code for reproduce.

https://github.com/zw963/marketbet_crawler/blob/test_snabberb/assets/js/app.rb

I use roda-sprockets plugin like this:

plugin :sprockets, precompile: %w(app.rb app.scss),
    root: Dir.pwd,
    public_path: 'public/',
    opal: true,
    debug: ENV['RACK_ENV'] != 'production'
tobymao commented 3 years ago

have you tried using opal-server and not sprockets? i can try debugging your repo later tonight, but this is a strange errror

zw963 commented 3 years ago

Okay, thank you, you may be require specify following three env to start repo.

RACK_ENV='development' DEVELOPMENT_DATABASE_URL=sqlite://db/files/marketbet_crawler_development.db APP_SESSION_SECRET="a692909f017cc94c96f8a1aff843d95920485376f4c997143cc3c39ca945c883ec88e310a2177a69b8b714d22af1b5fd7864833568b6bf93fc3bc811bcf6e112"

tobymao commented 3 years ago

Screenshot_2021-08-11_22 35 22

you need to wait for the dom to load before attaching the javascript. you put the javascript in the head, and it tries to run, but there is no div to attach to, so if you move the javascript below the body or wait for the dom to be ready it should work

zw963 commented 3 years ago

So cool! it works like a charm! Thank you.

zw963 commented 3 years ago

Hi, @tobymao , i am a newbie in Opal, can you please answer me another question?

That is, when use with snabberb, how the roda pass the data result (e.g. erb, or json) to snabberb?

could you please give me some clue? or a sample project. we can assume, i want to output a html table, which data come from roda/sequel stack. thank you.

tobymao commented 3 years ago

make an api request

here's an example of how i do it in another app

https://github.com/tobymao/18xx/blob/master/assets/app/lib/connection.rb#L60

zw963 commented 3 years ago

make an api request

here's an example of how i do it in another app

https://github.com/tobymao/18xx/blob/master/assets/app/lib/connection.rb#L60

Hi, i try with your's code, it not work.

image

i guess i do bad things again, anyway, if you have time, please give some clue.

following is my code.

https://github.com/zw963/marketbet_crawler/blob/test_send_request/assets/js/lib/request.rb

or, you can check out to remote test_send_request branch if repo still exists.

tobymao commented 3 years ago

@zw963 you don't want

data = data&.merge(_client_id: MessageBus.clientId), this is special code for my app

zw963 commented 3 years ago

@zw963 you don't want

data = data&.merge(_client_id: MessageBus.clientId), this is special code for my app

@tobymao , yes, i know, in fact, i just want use GET to retrieve some data for construct a table as following. after i remove data.&merge..., still get same error.

Request.send('/stocks', 'GET', {})

In fact, i even don't know where is the fetch method come from? i don't want to use too much gem , anyway, i use opal-browser gem resolve this, please check my code.

https://github.com/zw963/marketbet_crawler/blob/test_send_request/assets/js/app.rb#L14-L18

tobymao commented 3 years ago

fetch is. javascript method

On Sat, Aug 14, 2021 at 09:16 Billy.Zheng @.***> wrote:

@zw963 https://github.com/zw963 you don't want

data = data&.merge(_client_id: MessageBus.clientId), this is special code for my app

@tobymao https://github.com/tobymao , yes, i know, in fact, i just want use GET to retrieve some data for construct a table as following. after i remove data.&merge..., still get same error.

Request.send('/stocks', 'GET', {})

In fact, i even don't know where is the fetch method come from?

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/tobymao/snabberb/issues/7#issuecomment-898913875, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB6TF2QDTOHMLNOG27Q5253T42JGJANCNFSM5B7FQPBQ .

zw963 commented 3 years ago

fetch is. javascript method On Sat, Aug 14, 2021 at 09:16 Billy.Zheng @.***> wrote: @zw963 https://github.com/zw963 you don't want data = data&.merge(_client_id: MessageBus.clientId), this is special code for my app @tobymao https://github.com/tobymao , yes, i know, in fact, i just want use GET to retrieve some data for construct a table as following. after i remove data.&merge..., still get same error. Request.send('/stocks', 'GET', {}) In fact, i even don't know where is the fetch method come from? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#7 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB6TF2QDTOHMLNOG27Q5253T42JGJANCNFSM5B7FQPBQ .

Thank you for answer!

One more question please, i thought i can write out the html table use snabberb Virtual DOM h method now, but, i want to make this table can be sortable use one column too, suppose, :name, when click on link on table head row.

I guess there have to way to do this:

  1. add a trigger like JQuery, and then resort :name column.

  2. sort data from backend(roda), and then, use some mechanism update Virtual DOM automatically when data come from roda backend was updated.

I thought i can write out 1 if spend some time, but, i want to know, if there is a changes use snabberb do like 2? and how?

Thank you.

tobymao commented 3 years ago

you should be able to sort and rerender in snabberb directly without using jquery.

you can use an onclick handler

zw963 commented 3 years ago

you should be able to sort and rerender in snabberb directly without using jquery.

you can use an onclick handler

I guess it can be done like this, but, i familiar with backend, is there any mechanism for if the json data changes, will redraw all virtual DOM automatically? that said, backend render behavior.

tobymao commented 3 years ago

yes but if you want to do it like that, there was no reason to use javascript, just use erb and server side rendering

On Sun, Aug 15, 2021 at 00:53 Billy.Zheng @.***> wrote:

you should be able to sort and rerender in snabberb directly without using jquery.

you can use an onclick handler

I guess it can be done like this, but, i familiar with backend, is there any mechanism for if the json data changes, will redraw all virtual DOM automatically? that said, backend render behavior.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/tobymao/snabberb/issues/7#issuecomment-899011381, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB6TF2T3BSVBQEL5AH2CUELT45XANANCNFSM5B7FQPBQ .

zw963 commented 3 years ago

here was no reason to use javascript,

No, the different is user experience.

  1. Use Roda + ERB server side rending, it will always refresh page.
  2. Use Roda + JSON data, let Virtual DOM refresh only data was changed, browser not refresh full page.

Though, 2 compare to order use snabberb directly, need send a more request to server,but anyway, i can like many code use ruby from server side instead, and still give user a more frindly experience.