Jasonette / JASONETTE-Android

📡 Native App over HTTP, on Android
https://www.jasonette.com
Other
1.6k stars 270 forks source link

Updated policy for $show and $load #126

Closed gliechtenstein closed 7 years ago

gliechtenstein commented 7 years ago

Related: https://github.com/Jasonette/JASONETTE-iOS/pull/232

Problem

I have noticed a frequent usage pattern where the current $show and $load syntax cannot support easily.

This happens in the following case:

  1. The user load a static JASON markup WITHOUT doing a $render on load.
  2. The user also wants to make sure that the page is always fresh when the view comes back from another view.
  3. Normally people try to achieve this by setting a $show handler and doing a "type": "$reload", like this:
{
  "$jason": {
    "head": {
      "actions": {
        "$show": {
          "type": "$reload"
        }
      }
    },
    "body": {
      "sections": [{
        "items": [{
          ...
        }]
      }]
    }
  }
}

Notice how the body is immediately under $jason, which means everything is server-side rendered and there is no $render action.

What happens in this case is:

  1. Jasonette loads the initial markup and starts drawing it on the screen.
  2. And while step 1 is still in progress, Jasonette triggers the $reload action because $show was triggered.
  3. The result is Jasonette having to fetch exactly the same JSON from the server twice.
  4. This results in very poor performance both client side and server side (You are making two duplicate requests consecutively, as well as trying to render the same duplicate markup consecutively while the initial drawing is going on)

I can understand why people use this approach--because they want the view to be fresh everytime it shows up--and currently there is no easy way to distinguish between the initial $show event (triggered right after $load) and subsequent $show events (triggered when coming back from child views).

The current approach

Here's how it currently works:

The new approach

The new approach attempts to keep backwards compatibility while adding more flexibility. More specifically we're adding one more scenario (Handling when both $load and $show handlers exist)

  1. When there's only a $show handler

    • $show: Handles both initial load and subsequent show events
  2. When there's only a $load handler

    • $load: Handles Only the initial load event
  3. When there are both $show and $load handlers

    • $load : handle initial load only
    • $show : handle subsequent show events only

    Summary

Here's a demo: https://jasonbase.com/things/o2Q5