@thoughtbot/superglue 0.51.1 → 0.52.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -32
- package/package.json +1 -1
- package/utils/immutability.js +0 -4
package/README.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
<div align="center" style="padding: 30px 0px 20px 0px;">
|
|
2
|
+
<img src="https://thoughtbot.github.io/superglue/images/superglue.svg" data-origin="images/superglue.svg" alt="Logo" width=250>
|
|
3
|
+
</div>
|
|
4
|
+
|
|
1
5
|
# Superglue
|
|
2
6
|
|
|
3
7
|
Use classic Rails to build rich React Redux applications with **NO APIs** and
|
|
@@ -5,11 +9,11 @@ Use classic Rails to build rich React Redux applications with **NO APIs** and
|
|
|
5
9
|
|
|
6
10
|
[](https://circleci.com/gh/thoughtbot/superglue)
|
|
7
11
|
|
|
8
|
-
Superglue makes React and Redux
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
Superglue makes React and Redux as productive as Hotwire, Turbo and Stimulus.
|
|
13
|
+
Its inspired by Turbolinks and designed to feel like a natural extension of
|
|
14
|
+
Rails. Enjoy the benefits of Redux state management and React components
|
|
15
|
+
without giving up the productivity of Rails form helpers, UJS, tag helpers,
|
|
16
|
+
flash, cookie auth, and more.
|
|
13
17
|
|
|
14
18
|
## Caution
|
|
15
19
|
|
|
@@ -30,22 +34,22 @@ The end result would be something like this:
|
|
|
30
34
|

|
|
31
35
|
|
|
32
36
|
### Powered by Classic Rails
|
|
33
|
-
Superglue
|
|
37
|
+
Superglue leans on Rails. Features like the flash, cookie auth, and URL
|
|
34
38
|
helpers continue to be useful. Here's a look at the directory structure of a
|
|
35
39
|
typical Rails application with Superglue.
|
|
36
40
|
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
```treeview
|
|
42
|
+
app/
|
|
43
|
+
|-- controllers/
|
|
44
|
+
|-- views/
|
|
45
|
+
| |-- dashboard/
|
|
46
|
+
| | |-- index.js # The React page component
|
|
47
|
+
| | |-- index.json.props # The json for the page component
|
|
48
|
+
| | |-- index.html.erb
|
|
45
49
|
```
|
|
46
50
|
|
|
47
51
|
### PropsTemplate
|
|
48
|
-
Powering
|
|
52
|
+
Powering the JSON responses is PropsTemplate, a digable JSON templating DSL
|
|
49
53
|
inspired by JBuilder. With PropsTemplate you can specify a path of the node you
|
|
50
54
|
want, and PropsTemplate will walk the tree to it, skipping the execution of nodes
|
|
51
55
|
that don't match the keypath.
|
|
@@ -61,36 +65,38 @@ A popular ask of SPAs is page-to-page navigation without reloading. This is
|
|
|
61
65
|
easily done with Superglue's own UJS attributes inspired by Turbolinks:
|
|
62
66
|
|
|
63
67
|
```jsx
|
|
64
|
-
<a href='/posts' data-sg-visit
|
|
68
|
+
<a href='/posts' data-sg-visit />
|
|
65
69
|
```
|
|
66
70
|
|
|
67
71
|
The above will request for `/posts` with an `accept` of `application/json`, and
|
|
68
72
|
when the client receives the response, swap out the current component for the
|
|
69
73
|
component the response asks for, and `pushState` on history.
|
|
70
74
|
|
|
71
|
-
#### Partial updates
|
|
72
|
-
Some features rely on updating some parts of the existing page. In
|
|
73
|
-
addition to `data-sg-visit` and it's equivalent `this.props.visit`, Superglue
|
|
74
|
-
also provides `data-sg-remote` or `this.props.remote`, which you can use to
|
|
75
|
-
update parts of your page in async fashion without changing `window.history`.
|
|
76
75
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
76
|
+
#### Easy Partial updates
|
|
77
|
+
Some features rely on updating some parts of the existing page. Imagine
|
|
78
|
+
implementing type-ahead search. In traditional applications, you may need a new
|
|
79
|
+
controller, routes, a discussion over versioning, JSON serializer, plenty of
|
|
80
|
+
new JS code, etc.
|
|
81
81
|
|
|
82
82
|

|
|
83
83
|
|
|
84
|
-
With Superglue, this can be done
|
|
84
|
+
With Superglue, this can be done with a simple `onChange`
|
|
85
85
|
|
|
86
|
-
```
|
|
87
|
-
|
|
86
|
+
```js
|
|
87
|
+
const onChange = (e) => (
|
|
88
|
+
remote(`/dashboard?qry=${e.target.value}&props_at=data.header.search`)}
|
|
89
|
+
)
|
|
88
90
|
```
|
|
89
91
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
92
|
+
?> `remote` and `visit` is a thunk [passed] to every page component.
|
|
93
|
+
|
|
94
|
+
[passed]: ./navigation.md
|
|
95
|
+
|
|
96
|
+
With `props_at`, the above will make a request to `/dashboard?qry=haircut`,
|
|
97
|
+
dig your template for the `data.header.search` node, return it in the response,
|
|
98
|
+
and immutably graft it in the exact same path on the redux store before finally
|
|
99
|
+
letting React re-render.
|
|
94
100
|
|
|
95
101
|
For more on what you can do, check out our documentation.
|
|
96
102
|
|
package/package.json
CHANGED
package/utils/immutability.js
CHANGED
|
@@ -114,10 +114,6 @@ function atKey(node, key) {
|
|
|
114
114
|
throw new KeyPathError("Expected to find an Array when using the key: " + key);
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
-
if (isObject(node) && !Object.prototype.hasOwnProperty.call(node, key)) {
|
|
118
|
-
throw new KeyPathError("Expected to find key: " + key + " in object " + JSON.stringify(node));
|
|
119
|
-
}
|
|
120
|
-
|
|
121
117
|
if (Array.isArray(node) && id) {
|
|
122
118
|
var child;
|
|
123
119
|
|