|
| 1 | +# matestack core component: Isolate |
| 2 | + |
| 3 | +Show [specs](/spec/usage/components/isolate_spec.rb) |
| 4 | + |
| 5 | +The `isolate` component allows you to create isolated "scopes", which can be rendered without calling the main response method of a `page`. Used with `async`, only specific isolated scopes are resolved on the serverside rather than resolving the whole UI when asynchronously rerendering parts of the UI. Usage seems to be similar to `partial`, but there are important differences! Read below! |
| 6 | + |
| 7 | +**the isolate/async components currently only work on page-level --> we're working on it in order support the usage of async/isolate within a component [#75](https://github.com/basemate/matestack-ui-core/issues/75)** |
| 8 | + |
| 9 | +## Parameters |
| 10 | + |
| 11 | +The isolate core component accepts the following parameters: |
| 12 | + |
| 13 | +### isolated method (as a symbol) (mandatory) |
| 14 | + |
| 15 | +Just like a partial, using a symbol, a specific part of the UI can be referenced: |
| 16 | + |
| 17 | +```ruby |
| 18 | +def response |
| 19 | + components{ |
| 20 | + #a lot of other UI components you want to bypass if you rerender the isolated scope |
| 21 | + |
| 22 | + isolate :my_isolated_scope |
| 23 | + |
| 24 | + #a lot of other UI components you want to bypass if you rerender the isolated scope |
| 25 | + |
| 26 | + #for emphasizing the difference, a similar partial would work like so: |
| 27 | + partial :my_partial_scope |
| 28 | + } |
| 29 | +end |
| 30 | + |
| 31 | +def my_isolated_scope |
| 32 | + @some_data = SomeModel.find(42) |
| 33 | + |
| 34 | + isolate{ |
| 35 | + async rerender_on: "some_event" do |
| 36 | + div do |
| 37 | + plain @some_data.some_attribute |
| 38 | + end |
| 39 | + end |
| 40 | + } |
| 41 | +end |
| 42 | + |
| 43 | +def my_partial_scope |
| 44 | + @some_data = SomeModel.find(42) |
| 45 | + |
| 46 | + partial{ |
| 47 | + async rerender_on: "some_other_event" do |
| 48 | + div do |
| 49 | + plain @some_data.some_attribute |
| 50 | + end |
| 51 | + end |
| 52 | + } |
| 53 | +end |
| 54 | +``` |
| 55 | + |
| 56 | +As mentioned in the example code above: The `async` requests a new version of `my_isolated_scope` after the event `some_event` is received. But instead of resolving the whole UI and then returning only the desired part, the `isolate` component takes care of bypassing all irrelevant parts of the UI. The `async` rerender action inside the `partial` in contrast would call the whole `response` method of the page, which can lead to significant higher response times. |
| 57 | + |
| 58 | + |
| 59 | +### Cached Params (Clientside params --> public!) |
| 60 | + |
| 61 | +As the isolated scope is truly encapsulated from the main `response` method while rerendering, simply passing in dynamic params from within the `response` method can not work. As it is often necessary, to render "partials" dynamically, we can use `cached_params` for isolated scopes. These params are stored on the clientside after initial page and then used when performing the rerender action. Using this approach, we can still bypass the main `response` method while using dynamic params. |
| 62 | + |
| 63 | +**Just put simple data into cached_params (simple integers or strings, no hashes etc). Never put sensible data into cached_params as they are visible on the clientside!** |
| 64 | + |
| 65 | +```ruby |
| 66 | +def response |
| 67 | + components{ |
| 68 | + #a lot of other UI components you want to bypass if you rerender the isolated scope |
| 69 | + |
| 70 | + [1, 2, 3].each do |id| |
| 71 | + isolate :my_isolated_scope, cached_params: { id: id } |
| 72 | + end |
| 73 | + |
| 74 | + #a lot of other UI components you want to bypass if you rerender the isolated scope |
| 75 | + |
| 76 | + #for emphasizing the difference, a similar partial would work like so: |
| 77 | + [1, 2, 3].each do |id| |
| 78 | + partial :my_partial_scope, id |
| 79 | + end |
| 80 | + } |
| 81 | +end |
| 82 | + |
| 83 | +def my_isolated_scope cached_params |
| 84 | + @some_data = SomeModel.find(cached_params[:id]) |
| 85 | + |
| 86 | + isolate{ |
| 87 | + async rerender_on: "isolated_rerender_#{cached_params[:id]}" do |
| 88 | + div do |
| 89 | + plain @some_data.some_attribute |
| 90 | + end |
| 91 | + end |
| 92 | + } |
| 93 | +end |
| 94 | + |
| 95 | +def my_partial_scope id |
| 96 | + @some_data = SomeModel.find(id) |
| 97 | + |
| 98 | + partial{ |
| 99 | + async rerender_on: "partial_rerender_#{id}" do |
| 100 | + div do |
| 101 | + plain @some_data.some_attribute |
| 102 | + end |
| 103 | + end |
| 104 | + } |
| 105 | +end |
| 106 | +``` |
0 commit comments