{"id":19529895,"url":"https://github.com/loopmode/bind","last_synced_at":"2025-02-26T02:43:11.234Z","repository":{"id":140399620,"uuid":"160649876","full_name":"loopmode/bind","owner":"loopmode","description":"Helper for binding the scope of class methods","archived":false,"fork":false,"pushed_at":"2020-02-03T12:56:32.000Z","size":278,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-08T18:36:50.012Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/loopmode.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-12-06T09:19:30.000Z","updated_at":"2020-02-03T12:56:34.000Z","dependencies_parsed_at":null,"dependency_job_id":"0db35a87-478f-44ab-830e-75945e211fbb","html_url":"https://github.com/loopmode/bind","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loopmode%2Fbind","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loopmode%2Fbind/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loopmode%2Fbind/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loopmode%2Fbind/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/loopmode","download_url":"https://codeload.github.com/loopmode/bind/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240783109,"owners_count":19856776,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T01:28:03.478Z","updated_at":"2025-02-26T02:43:11.182Z","avatar_url":"https://github.com/loopmode.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @loopmode/bind\n\nA scope binding mechanism for javascript classes.\n\n\u003cbr/\u003e\n\n## Default matcher\n\nOut of the box, the function will bind all methods that have a name starting with `on` followed by uppercase character, or `handle` followed by either uppercase character or underscore.\n\nYou are encouraged to use a naming convention like that (e.g. `handleClick`), because then you don't need to pass any arguments but the class instance itself:\n\n```javascript\nimport bind from '@loopmode/bind';\n\nexport class Demo {\n    constructor() {\n        bind(this);\n        window.addEventListener('click', this.handleClick);\n    }\n    handleClick(event) {\n        console.log('handleClick', this, event);\n    }\n}\n```\n\n## Special matchers\n\n-   `*` bind all methods, regardless of their name\n-   A matcher can be a function that receives the function name and returns a boolean\n\n## Custom matchers\n\nYou can also pass additional arguments to specify custom matchers:\n\n```javascript\nimport bind from '@loopmode/bind';\n\nexport class Demo {\n    constructor() {\n        bind(this, /^handle/, 'init');\n    }\n    init() {\n        window.addEventListener('click', this.handleClick);\n        window.addEventListener('change', this.handleChange);\n    }\n    destroy() {\n        window.removeEventListener('click', this.handleClick);\n        window.removeEventListener('change', this.handleChange);\n    }\n    handleClick(event) {\n        console.log('handleClick', this, event);\n    }\n    handleChange(event) {\n        console.log('handleChange', this, event);\n    }\n}\n\nconst demo - new Demo();\ndocument.addEventListener('DOMContentLoaded', demo.init)\n```\n\nSome more examples:\n\n```javascript\nbind(this, /^on/); // match `onClick`, `onChange` etc, using RegExp object\n\nbind(this, '^on'); // same as before, but with string regex\n\nbind(this, 'on'); // string matcher, will match both `onClick` and e.g. `createBaboon`\n\nbind(this, 'on$'); // Will match `createBaboon` but not `onClick`\n\nbind(this, /^handle/, /^on/); // Will match all methods starting with `handle` or `on`, multiple arguments\n\nbind(this, [/^handle/, /^on/]); // Same as before, but with a single array as argument\n\nbind(this, [/^handle/, 'renderConfirmDialog']); // Typical real-world-case, match handlers but also some specific render method that gets injected into a child\n```\n\n## Custom wrapper\n\nIn case you do have a naming convention, but it's not `handle*`, you should create your own module with a wrapper function that provides the appropriate matcher, and use that - without passing extra arguments.\u003cbr/\u003e\nFor example, if you typically use `onEvent` rather than `handleEvent`, your own `bind` module might export this wrapper function:\n\n```javascript\nimport bind from '@loopmode/bind';\nexport default instance =\u003e bind(instance, /^on/);\n```\n\n## React components\n\nYou will not run into troubles with e.g. React components - lifecycle methods are not bound unless you defined an explicit matcher for that (e.g. `render` or `/^componentDid/` etc).\n\n```javascript\nimport React from 'react';\nimport bind from '@loopmode/bind';\n\nexport class bind extends React.Component {\n    state = {\n        clickCount: 0\n    };\n    constructor(props) {\n        super(props);\n        bind(this);\n    }\n    render() {\n        return \u003cbutton onClick={this.handleClick}\u003eclick me\u003c/button\u003e;\n    }\n    handleClick(event) {\n        this.setState({ clickCount: this.state.clickCount + 1 });\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floopmode%2Fbind","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Floopmode%2Fbind","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floopmode%2Fbind/lists"}