Skip to content

Commit 20dedfe

Browse files
authored
RmJQuery: replace Backbone.ajax (#1453)
1 parent e40e1e7 commit 20dedfe

File tree

7 files changed

+18
-39
lines changed

7 files changed

+18
-39
lines changed

app/javascript/application.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// @ts-nocheck
22
import "@hotwired/turbo-rails";
33
import "@rails/activestorage";
4-
import "jquery";
54
import * as bootstrap from "bootstrap";
65

76
window.bootstrap = bootstrap;
@@ -14,12 +13,25 @@ import "./controllers/index";
1413

1514
Turbo.session.drive = false;
1615

17-
/* global jQuery, Mousetrap */
18-
var $ = jQuery;
16+
/* global Mousetrap */
1917

20-
window.$ = $;
21-
22-
Backbone.$ = $;
18+
Backbone.ajax = async function(options) {
19+
try {
20+
const response = await fetch(options.url, {
21+
body: options.data,
22+
headers: {"Content-Type": "application/json", ...options.headers},
23+
method: options.type || "GET",
24+
});
25+
const data = await response.json();
26+
if (response.ok && options.success) {
27+
options.success(data, response.statusText, response);
28+
} else if (!response.ok && options.error) {
29+
options.error(response, response.statusText, null);
30+
}
31+
} catch (err: unknown) {
32+
if (options.error) options.error(null, "error", err);
33+
}
34+
};
2335

2436
_.templateSettings = {
2537
evaluate: /\{\{(.+?)\}\}/g,

app/javascript/support/jquery-global.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

bin/build

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ esbuild app/javascript/*.ts \
88
--format=iife \
99
--outdir=app/assets/builds \
1010
--public-path=/assets \
11-
--alias:jquery=./app/javascript/support/jquery-global.ts \
1211
--alias:bootstrap=./node_modules/bootstrap/dist/js/bootstrap.bundle.js \
1312
"$@"

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"backbone.nativeview": "^0.3.4",
1414
"bootstrap": "^5.3.3",
1515
"font-awesome": "4.7.0",
16-
"jquery": "2.2.4",
1716
"mousetrap": "1.4.6",
1817
"underscore": "1.13.8"
1918
},
@@ -23,7 +22,6 @@
2322
"@stylistic/eslint-plugin": "^5.7.0",
2423
"@types/backbone": "latest",
2524
"@types/hotwired__turbo": "^8.0.5",
26-
"@types/jquery": "3.5.34",
2725
"@types/mousetrap": "latest",
2826
"@types/node": "^25.2.3",
2927
"@types/rails__actioncable": "^8.0.3",

pnpm-lock.yaml

Lines changed: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/javascript/setup.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
// @ts-nocheck
22
/// <reference types="vitest/globals" />
33

4-
import "jquery";
54
import underscore from "underscore";
65
import Backbone from "backbone";
76

87
beforeEach(() => {
98
expect.hasAssertions();
109
});
1110

12-
const jquery = window.jQuery;
13-
globalThis.$ = jquery;
14-
globalThis.jQuery = jquery;
1511
globalThis._ = underscore;
1612
globalThis.Backbone = Backbone;
1713

18-
Backbone.$ = jquery;
19-
2014
_.templateSettings = {
2115
evaluate: /\{\{(.+?)\}\}/g,
2216
interpolate: /\{\{=(.+?)\}\}/g,

vitest.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export default defineConfig({
1515
{find: /^helpers\//u, replacement: appPath("helpers")},
1616
{find: /^javascript\//u, replacement: appPath("")},
1717
{find: /^spec\//u, replacement: `${path.resolve(root, "spec")}/`},
18-
{find: "jquery", replacement: path.resolve(root, "app/javascript/support/jquery-global.ts")},
1918
{find: "bootstrap", replacement: path.resolve(root, "node_modules/bootstrap/dist/js/bootstrap.js")},
2019
{find: "mousetrap", replacement: path.resolve(root, "node_modules/mousetrap/mousetrap.js")},
2120
{

0 commit comments

Comments
 (0)