{"id":26068351,"url":"https://github.com/minutebase/ember-contextual-services","last_synced_at":"2025-03-08T22:25:19.296Z","repository":{"id":57223589,"uuid":"82571590","full_name":"minutebase/ember-contextual-services","owner":"minutebase","description":"Contextual Services for Instances","archived":false,"fork":false,"pushed_at":"2018-04-19T14:02:09.000Z","size":88,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-11T23:01:50.630Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/minutebase.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-20T15:15:26.000Z","updated_at":"2018-04-19T14:02:11.000Z","dependencies_parsed_at":"2022-08-30T02:40:20.311Z","dependency_job_id":null,"html_url":"https://github.com/minutebase/ember-contextual-services","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minutebase%2Fember-contextual-services","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minutebase%2Fember-contextual-services/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minutebase%2Fember-contextual-services/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minutebase%2Fember-contextual-services/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/minutebase","download_url":"https://codeload.github.com/minutebase/ember-contextual-services/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240206990,"owners_count":19765041,"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":"2025-03-08T22:25:18.819Z","updated_at":"2025-03-08T22:25:19.287Z","avatar_url":"https://github.com/minutebase.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ember-contextual-services\n\nServices, for instances.\n\n## Installation\n\n```\nember install ember-contextual-services\n```\n\n## Usage\n\nFirst define an instance service for an object type, eg for an ember-data `Person`:\n\n```javascript\n// app/contextual-services/person.js\nimport ContextualService from 'ember-contextual-services';\nimport computed from 'ember-computed';\n\nexport default ContextualService.extend({\n  fullName: computed('model.firstName', 'model.lastName', function() {\n    return [this.get('model.firstName'), this.get('model.lastName')].join(' ');\n  })\n});\n```\n\nThen we can inject the `contextual-service` service and fetch the instance service for an object using `serviceFor`:\n\n```javascript\n// app/components/x-person/component.js\nimport Component from 'ember-component';\nimport service from 'ember-service/inject';\n\nexport default Component.extend({\n  contextualService: service(),\n  person: null,\n\n  doSomething() {\n    const personService = this.get('contextualService').serviceFor(this.get('person'));\n    personService.get('fullName');\n  }\n});\n```\n\nAs a shorthand, instead of injecting and looking up using the `contextual-service` we can use `serviceFor` which is syntactical sugar for the same:\n\n```javascript\n// app/components/x-person/component.js\nimport Component from 'ember-component';\nimport { serviceFor } from 'ember-contextual-services';\n\nexport default Component.extend({\n  personService: serviceFor('person'),\n  person: null,\n\n  doSomething() {\n    return this.get('personService').get('fullName');\n  }\n});\n```\n\nOr in templates we can use the `contextual-service` helper:\n\n```handlebars\n{{get (contextual-service person) 'fullName'}}\n```\n\nFor simple properties, this is a bit overkill, but where it comes into its own is in conjunction with ember-concurrency:\n\n```javascript\n// app/contextual-services/person.js\nimport ContextualService from 'ember-contextual-services';\nimport { task, timeout } from 'ember-concurrency';\n\nexport default ContextualService.extend({\n  debouncedSave: task(function * () {\n    yield timeout(1000)\n    yield this.get('model').save();\n  }).restartable();\n});\n```\n\n## Contexts\n\n`serviceFor` takes a 2nd parameter as a \"context\" so you can split out functionality for an object type across separate files.\n\nFor example `serviceFor(person, 'display')` will lookup the service from `app/contextual-services/person/display.js` instead of just `app/contextual-services/person.js`.\n\n## Service lookup\n\nBy default `serviceFor` will use the ember-data `modelName` in order to find the service.\n\nYou can change this by extending the service and overriding `contextualServiceScope`:\n\n```javascript\nimport ContextualServicesService from 'ember-contextual-services/services/contextual-service';\nexport default ContextualServicesService.extend({\n  contextualServiceScope(model) {\n    return 'foo';\n  }\n});\n```\n\nserviceFor uses the `guid` as the id so that the same service instance is returned each time for the same object.\n\nAgain this can be changed by overriding `contextualServiceID`, for example using the `id` instead:\n\n```javascript\nimport ContextualServicesService from 'ember-contextual-services/services/contextual-service';\nexport default ContextualServicesService.extend({\n  contextualServiceID(model) {\n    return model.get('key');\n  }\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fminutebase%2Fember-contextual-services","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fminutebase%2Fember-contextual-services","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fminutebase%2Fember-contextual-services/lists"}