Andrew Krigline
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Planning our Module ###### tags: `module-tutorial` `foundry-vtt` Let's take a step back and think about the structure of what our `todo-list` module should look like when it's done. ## I. ESModules vs Scripts There are two ways we can tell Foundry Core about a module's javascript files in its Manifest: `"scripts"` and `"esmodules"`. There's a lot of philosophical difference between these approaches but we're going to focus on the practical here. Practically speaking, the difference comes down to the scope in which your script is run. :::info **Programming Concept: Scope** Think of "scope" in this context like a room in an office building where a script is allowed to work. ::: ### Scripts Since the beginning of javascript, scripts have been loaded all together and all at once and expected to co-exist in a single global 'scope'. Putting all of the scripts in the same scope has both benefits and drawbacks. - For simple cases (like a website which needs only 1 or 2 scripts) it's simpler and faster. - For cases where scripts want to work together, working in the same scope is easy. - The more scripts you add, the more likely a conflict will arise where Script F overrides something Script B was using, etc. > Office Example: > When there's only one or two people working together in a room things work great. They can share a whiteboard, table space, etc. without getting in eachother's way. > > When there's a _lot_ of people working together in a single room, things get complicated. Conflicts might arise where multiple individuals need the whiteboard or the table at once. And that one person comes along who starts eating other peoples' lunches. ### ES Modules Recently there has been a shift in javascript development away from putting all scripts in the same scope and instead introducing "modules" which can import eachother to work together. A single "module" in this context is a single "script." Each "script" in this setup works within its own scope with no overlap between them. This solves a lot of problems, but also has some drawbacks: - For simple cases, this is probably overkill. - For cases where scripts want to work together, they must explicitly define which parts of themselves are exported to be importable by other scripts. - It doesn't matter how many scripts you add, they will never conflict with eachother. > Office Example: > ES Modules are like cubicles. The office room got so hectic that to make things easier, every person gets their own mini-room within the larger room in which to work. > > When someone wants to interact with another person, they have to go to that other person and ask for stuff from the cubicle entrace. Only the things that the person inside the cubicle wants to can leave it. There's a lot more to unpack about this topic and I'll refer you to [this article](https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/) if you want to know more. ### Why Scripts? I chose to leverage scripts for this project because it is a simpler concept to grasp. Since we're not making an overly complex module, we're going to stick to scripts with some Object Oriented principles to help protect ourself and the other modules around us. ## II. Object Oriented Javascript? There's a lot of very philosophical talk out there among professional javascript developers on frontend, backend, and anywhere in-between about topics like this. Mozilla has an excellent [tutorial](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_JS) going into more details if you are interested. I will leave you with why I chose to write this tutorial as following some basic Object Oriented principles: 1. It's a way to keep your code safe from other peoples' code. All of the modules active in a Foundry instance are sharing the same "scope." This can lead to unexpected problems if your module is installed alongside another module. 2. This methodology draws inspiration from how Core is laid out. By working in a way that echos how Core works, you can get a better feeling for how to read Core code when you need to. ### The Basics Everything you care about should be grouped into `Class`es. A [`Class`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class) governs one 'concern'. These `Class`es are namespaced in such a way that they are easy to find and differentiate. Classes have `methods`/`properties` defined on their namespace. Some of these are `static`, which means they are accessible from outside the scope of the Class (most are not). :::info **Programming Concept: Terminology** There's a lot of overlapping teminology in Javascript-land. It's easy to get lost when the words switch half-way through a tutorial and I'm sure I'll end up doing that here. A [`method`](https://developer.mozilla.org/en-US/docs/Glossary/Method) is a [`function`](https://developer.mozilla.org/en-US/docs/Glossary/Function) which belongs to a Class (aka is a ["property"](https://developer.mozilla.org/en-US/docs/Glossary/property/JavaScript) of that Class). ::: --- ## III. Moving Parts At the most fundamental level, our TODO module needs two things: - A place to persist our data in the database. - A UI to interact with that data. We can break this task up and do things one at a time. We'll handle our data problem first, then make a UI later. :::info **Programming Concept: Separation of Concerns** From a theoretical standpoint, how we store the data should not care about the user interface. We should instead build an API for our data 'layer' which can be used by any number of consumers. For example, if done correctly a todo could be created, updated, or deleted by a macro, or by another module. ::: --- ## IV. Data Layer There's a few ways modules can [handle data](https://foundryvtt.wiki/en/development/guides/handling-data) in Foundry, which you use depends on your needs. Here's our assumptions: - Any user can create, edit, or delete a todo. - A todo can only be viewed/edited/deleted by its creator. - A todo should follow the user to whichever machine they log into. A `'client'` scoped setting almost fills these requirements and is a good option. However, since we want these todos to follow the user when the log in on a different machine, that won't work. Instead, we'll use a `flag` on the `User` Document to store todos. ### What is a `ToDo`? ```js= /** * A single ToDo in our list of Todos. * @typedef {Object} ToDo * @property {string} id - A unique ID to identify this todo. * @property {string} label - The text of the todo. * @property {boolean} isDone - Marks whether the todo is done. * @property {string} userId - The user's id which owns this ToDo. */ ``` This is a [jsdoc](https://jsdoc.app) [`@typedef`](https://jsdoc.app/tags-typedef.html) which defines the structure of our `ToDo` object. It notes that the ToDo is an object with four keys (`id`, `label`, `isDone`, `userId`) as well as what each of these do. An [IDE](https://developer.mozilla.org/en-US/docs/Glossary/IDE) like [Visual Studio Code](https://code.visualstudio.com/) has built-in tooling to make use of this kind of in-code documentation. ### What do we need to be able to do with it? | Description | Operation | | ---------------------------------------------- | ----------- | | User should be able to make new ToDos | Create | | User should be able to view one of their ToDos | Read | | An existing ToDo's `label` should be editable | Edit Label | | An existing ToDo should be mark-able as "Done" | Toggle Done | | User should be able to delete ToDos. | Delete | :::info **Programming Concept: CRUD** **C**reate, **R**ead, **U**pdate, and **D**elete is often abbreviated as simply **"CRUD"** In our table of operations above, "Edit Label" and "Toggle Done" are both really the same data operation: "Update and existing ToDo" ::: --- ## V. Recap We want a User Interface which allows Users to Create, Read, Update, and Delete `ToDo`s which are objects consisting of some Text for a `label` and a Boolean for `isDone` stored on `User` `flags`. Next Step: [Setting Up](/ojFSOsrNTySh9HbzTE3Orw) {%hackmd io_aG7zdTKyRe3cpLcsxzw %}

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully