Skip to content

Discussion - Relative URL support #32

@gliechtenstein

Description

@gliechtenstein

@brad came up with a great feature that takes care of relative URLs. Here's the PR: Jasonette/JASONETTE-Android#35

The solution basically:

  1. Looks at the url attribute of the strings.xml (The same url used for the root url of the app itself)
  2. Use that as the root url with which all the relative URLs in the app will be resolved.

So for example, if the app's root URL is https://www.jasonbase.com/things/nfk.json, you would be able to specify just the relative path throughout the app like this:

{
  "type": "image",
  "url": "/avatar.png"
}

I like the idea of being able to make the URLs domain independent, which would make the JSON code much more reusable and powerful.

I have a couple of thoughts on this, and also wanted to put this out there for public discussion before we make any decision.

Discussion 1. one root_url per view?

Maybe we can make this more flexible by stating the root url inside the JSON instead of using the hardcoded value from strings.xml? That way instead of having a single global root_url, we can namespace them per view. Also it's 100% powered by the JSON instead of hardcoded values. Here's an example:

{
  "$jason": {
    "head": {
      "root_url": "https://www.jasonbase.com/things",
      "title": "relative path demo",
      ...
    },
    "body": {
      "sections": [{
        "items": [{
          "type": "label",
          "text": "go",
          "href": {
            "url": "/f3h.json"
          }
        }]
      }]
    }
  }
}

Discussion 2. Would this type of approach make the code less readable?

Upon thinking more about this I've come up with some concerns. One of the coolest things about Jasonette is you can basically walk through all the referenced JSON to figure out how the entire app works.

But maybe adding a relative url element puts more burden on people trying to figure out what the JSON does? After all, we're adding one more step for users to think about whenever they come across a url. I'm not suggesting this is a serious problem, but just wanted to bring this to the table and hear what others think.

Discussion 3. Can we use templates to achieve similar effect?

Going back to above example, maybe we can achieve the same effect without introducing new concepts. For example we can $set a local variable called root and use that inside templates. Not exactly as succinct as the original solution suggested by brad but it does make the code more modular and shareable:

{
  "$jason": {
    "head": {
      "root_url": "https://www.jasonbase.com/things",
      "title": "relative path demo",
      "actions": {
        "$load": {
          "type": "$set",
          "options": {
            "root": "https://www.jasonbase.com/things"
          },
          "success": {
            "type": "$render"
          }
      },
      "templates": {
        "body": {
          "sections": [{
            "items": [{
              "type": "label",
              "text": "go",
              "href": {
                "url": "{{$get.root}}/f3h.json"
              }
            }]
          }]
        }
      },
      ...
    }
  }
}

What do you guys think? Any ideas/feedback welcome!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions