Skip to content

Commit fff122b

Browse files
params as instance methods
1 parent 2e68f4a commit fff122b

File tree

1 file changed

+38
-39
lines changed

1 file changed

+38
-39
lines changed

docs/dsl-client/hyper-component.md

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ Parent { Child() }
222222
param :items
223223
render do
224224
# notice how the items param is accessed in CamelCase (to indicate that it is read-only)
225-
@Items.each do |item|
225+
items.each do |item|
226226
PARA do
227227
item[:text]
228228
end
@@ -241,7 +241,7 @@ The situation gets more complicated when the children are shuffled around (as in
241241
param :results, type: [Hash] # each result is a hash of the form {id: ..., text: ....}
242242
render do
243243
OL do
244-
@Results.each do |result|
244+
results.each do |result|
245245
LI(key: result[:id]) { result[:text] }
246246
end
247247
end
@@ -257,15 +257,15 @@ The `key` should *always* be supplied directly to the components in the array, n
257257
class ListItemWrapper < HyperComponent
258258
param :data
259259
render do
260-
LI(key: @Data[:id]) { @Data[:text] }
260+
LI(key: data[:id]) { data[:text] }
261261
end
262262
end
263263

264264
class MyComponent < HyperComponent
265265
param :results
266266
render do
267267
UL do
268-
@Result.each do |result|
268+
result.each do |result|
269269
ListItemWrapper data: result
270270
end
271271
end
@@ -278,15 +278,15 @@ end
278278
class ListItemWrapper < HyperComponent
279279
param :data
280280
render do
281-
LI { @Data[:text] }
281+
LI { data[:text] }
282282
end
283283
end
284284

285285
class MyComponent < HyperComponent
286286
param :results
287287
render do
288288
UL do
289-
@Result.each do |result|
289+
results.each do |result|
290290
ListItemWrapper key: result[:id], data: result
291291
end
292292
end
@@ -316,7 +316,7 @@ class IndentEachLine < HyperComponent
316316

317317
render(DIV) do
318318
children.each_with_index do |child, i|
319-
child.render(style: {"margin-left" => @By*i})
319+
child.render(style: {"margin-left" => by*i})
320320
end
321321
end
322322
end
@@ -336,10 +336,9 @@ When designing interfaces, break down the common design elements (buttons, form
336336

337337
## Params
338338

339-
The `param` method gives *read-only* access to each of the scalar params passed to the Component. The params are accessed as instance variables converted to CamelCase.
339+
The `param` method gives *read-only* access to each of the scalar params passed to the Component. Params are accessed as instance methods on the Component.
340340

341-
Within a React Component the `param` method is used to define the parameter signature of the component. You can think of params as
342-
the values that would normally be sent to the instance's `initialize` method, but with the difference that a React Component gets new parameters when it is re-rendered.
341+
Within a React Component the `param` method is used to define the parameter signature of the component. You can think of params as the values that would normally be sent to the instance's `initialize` method, but with the difference that a React Component gets new parameters when it is re-rendered.
343342

344343
Note that the default value can be supplied either as the hash value of the symbol, or explicitly using the `:default_value` key.
345344

@@ -358,7 +357,7 @@ param foo: [], type: [String] # foo must be an array of strings, and has a
358357

359358
#### Accessing param values
360359

361-
> Params are accessible in the Component class as instance variables in **CamelCase**. The CamelCase syntax is used to indicate that params are immutable.
360+
Params are accessible in the Component class as instance methods.
362361

363362
For example:
364363

@@ -368,7 +367,7 @@ class Hello < HyperComponent
368367
param visitor: "World", type: String
369368

370369
render do
371-
"Hello #{@Visitor}" # notice how you CamelCase for immutable params
370+
"Hello #{visitor}"
372371
end
373372
end
374373
```
@@ -389,8 +388,8 @@ class Likes < HyperComponent
389388
param :book # book is an instance of the Book model
390389

391390
render(DIV) do
392-
P { "#{@Book.likes.count} likes" }
393-
BUTTON { "Like" }.on(:click) { @Book.likes += 1}
391+
P { "#{book.likes.count} likes" }
392+
BUTTON { "Like" }.on(:click) { book.likes += 1}
394393
end
395394
end
396395
```
@@ -433,7 +432,7 @@ A Ruby `Proc` can be passed to a component like any other object. A param of ty
433432
param :all_done, type: Proc
434433
...
435434
# typically in an event handler
436-
params.all_done(data) # instead of params.all_done.call(data)
435+
all_done(data).call
437436
```
438437

439438
Proc params can be optional, using the `default: nil` and `allow_nil: true` options. Invoking a nil proc param will do nothing. This is handy for allowing optional callbacks.
@@ -445,8 +444,8 @@ class Alarm < HyperComponent
445444

446445
after_mount do
447446
@clock = every(1) do
448-
if Time.now > @At
449-
@Notify
447+
if Time.now > at
448+
notify.call
450449
@clock.stop
451450
end
452451
force_update!
@@ -475,7 +474,7 @@ class ButtonBar < HyperComponent
475474
param :button
476475

477476
render do
478-
@Button.render
477+
button.render
479478
end
480479
end
481480
```
@@ -491,10 +490,10 @@ class Test < HyperComponent
491490
render do
492491
DIV do
493492
children.each do |child|
494-
@Node.render
493+
node.render
495494
child.render
496495
end
497-
@Node.render
496+
node.render
498497
end
499498
end
500499
end
@@ -511,7 +510,7 @@ class CheckLink < HyperComponent
511510
collect_other_params_as :attributes
512511
render do
513512
# we just pass along any incoming attributes
514-
a(@Attributes) { ''.span; children.each &:render }
513+
a(attributes) { ''.span; children.each &:render }
515514
end
516515
end
517516
# CheckLink(href: "/checked.html")
@@ -656,7 +655,7 @@ class UsingState < HyperComponent
656655

657656
def output
658657
# rerender whenever input_value changes
659-
P { "#{@input_value}" }
658+
P { "#{@input_value}" }
660659
end
661660

662661
def easter_egg
@@ -1497,7 +1496,7 @@ class UserDialog < HyperComponent
14971496
end
14981497
DialogContent do
14991498
content
1500-
error_messages if @User.errors.any?
1499+
error_messages if user.errors.any?
15011500
end
15021501
DialogActions do
15031502
actions
@@ -1506,7 +1505,7 @@ class UserDialog < HyperComponent
15061505
end
15071506

15081507
def edit_or_new_button
1509-
if @User.new?
1508+
if user.new?
15101509
Fab(size: :small, color: :primary) { Icon { 'add' } }
15111510
else
15121511
Fab(size: :small, color: :secondary) { Icon { 'settings' } }
@@ -1516,11 +1515,11 @@ class UserDialog < HyperComponent
15161515
def content
15171516
FormGroup(row: true) do
15181517
# note .to_s to specifically cast to a String
1519-
TextField(label: 'First Name', value: @User.first_name.to_s).on(:change) do |e|
1520-
@User.first_name = e.target.value
1518+
TextField(label: 'First Name', value: user.first_name.to_s).on(:change) do |e|
1519+
user.first_name = e.target.value
15211520
end
1522-
TextField(label: 'Last Name', value: @User.last_name.to_s).on(:change) do |e|
1523-
@User.last_name = e.target.value
1521+
TextField(label: 'Last Name', value: user.last_name.to_s).on(:change) do |e|
1522+
user.last_name = e.target.value
15241523
end
15251524
end
15261525

@@ -1529,11 +1528,11 @@ class UserDialog < HyperComponent
15291528
FormLabel(component: 'legend') { 'Gender' }
15301529
RadioGroup(row: true) do
15311530
FormControlLabel(label: 'Male',
1532-
control: Radio(value: false, checked: !is_checked(@User.is_female)).as_node.to_n)
1531+
control: Radio(value: false, checked: !is_checked(user.is_female)).as_node.to_n)
15331532
FormControlLabel(label: 'Female',
1534-
control: Radio(value: true, checked: is_checked(@User.is_female)).as_node.to_n)
1533+
control: Radio(value: true, checked: is_checked(user.is_female)).as_node.to_n)
15351534
end.on(:change) do |e|
1536-
@User.is_female = e.target.value
1535+
user.is_female = e.target.value
15371536
end
15381537
end
15391538

@@ -1545,34 +1544,34 @@ class UserDialog < HyperComponent
15451544
def actions
15461545
Button { 'Cancel' }.on(:click) { cancel }
15471546

1548-
if @User.changed? && validate_content
1549-
Button(color: :primary, variant: :contained, disabled: (@User.saving? ? true : false)) do
1547+
if user.changed? && validate_content
1548+
Button(color: :primary, variant: :contained, disabled: (user.saving? ? true : false)) do
15501549
'Save'
15511550
end.on(:click) { save }
15521551
end
15531552
end
15541553

15551554
def save
1556-
@User.save(validate: true).then do |result|
1555+
user.save(validate: true).then do |result|
15571556
mutate @open = false if result[:success]
15581557
end
15591558
end
15601559

15611560
def cancel
1562-
@User.revert
1561+
user.revert
15631562
mutate @open = false
15641563
end
15651564

15661565
def error_messages
1567-
@User.errors.full_messages.each do |message|
1566+
user.errors.full_messages.each do |message|
15681567
Typography(variant: :h6, color: :secondary) { message }
15691568
end
15701569
end
15711570

15721571
def validate_content
1573-
return false if @User.first_name.to_s.empty?
1574-
return false if @User.last_name.to_s.empty?
1575-
return false if @User.is_female.nil?
1572+
return false if user.first_name.to_s.empty?
1573+
return false if user.last_name.to_s.empty?
1574+
return false if user.is_female.nil?
15761575

15771576
true
15781577
end

0 commit comments

Comments
 (0)