rails / request.js

MIT License
386 stars 28 forks source link

Support for responseKind js/script #48

Open PedroAugustoRamalhoDuarte opened 2 years ago

PedroAugustoRamalhoDuarte commented 2 years ago

In my project we use ajax to handle request in stimulus and we are trying to update to request.js:

Example:

$.ajax({
  beforeSend: function (xhr) {
    xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
  },
  url: url,
  remote: true,
  dataType: 'script',
  type: 'get',
});

For this we need this dataType script to execute js files, but in the request.js docs there are only 3 options for responseKind (html, turbo-stream, json). A new script option would be interesting

marcelolx commented 1 year ago

Could you maybe return a turbo-stream that contains a script tag that will be executed once the stream is inserted into the DOM? I know Turbo supports this https://github.com/hotwired/turbo/pull/192

I'm just not a huge fan of executing JS returned by the server this way, so I'm a bit hesitant about adding support for it https://github.com/hotwired/turbo/issues/186#issuecomment-959814926

PedroAugustoRamalhoDuarte commented 1 year ago

@marcelolx Maybe is a good replacement, what are the advantages and disadvantages for each options?

shufeilei commented 5 months ago

while moving to turbo-stream may be the ultimate destination, i second the addition of JS support at the mean time because old applications, like the one that i support, still runs on the old JS partials architecture. having the JS support allows me to keep the old code and make the migration more easily.

marcelolx commented 4 months ago

If someone is willing to put a PR together, I am happy to review and consider it (I don't have the bandwidth to work on it right now)

songjiz commented 4 months ago

I opened a PR https://github.com/rails/request.js/pull/72/files