Skip to content

Commit

Permalink
feat(core): add sugar function for model binding
Browse files Browse the repository at this point in the history
  • Loading branch information
shimks committed Jan 4, 2018
1 parent b70f55a commit 9a4b6d8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/core/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ export class Application extends Context {
.tag('controller');
}

/**
* Register a model class with this application.
*
* @param modelCtor {Function} The model class
* (constructor function).
* @return {Binding} The newly created binding, you can use the reference to
* further modify the binding, e.g. lock the value to prevent further
* modifications.
*
* ```ts
* class MyModel {
* }
* app.model(MyModel).lock();
* ```
*/
model(modelCtor: Constructor<{}>): Binding {
return this.bind(`models.${modelCtor.name}`)
.toClass(modelCtor)
.tag('model');
}

/**
* Bind a Server constructor to the Application's master context.
* Each server constructor added in this way must provide a unique prefix
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ export function mountComponent(app: Application, component: Component) {
}
}

if (component.models) {
for (const modelCtor of component.models) {
app.model(modelCtor);
}
}

if (component.providers) {
for (const providerKey in component.providers) {
app.bind(providerKey).toProvider(component.providers[providerKey]);
Expand Down
16 changes: 16 additions & 0 deletions packages/core/test/acceptance/application.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ describe('Bootstrapping the application', () => {
]);
});

it('registers all models from components', async () => {
class ProductModel {}

class ProductComponent {
models = [ProductModel];
}

const app = new Application({
components: [ProductComponent],
});

expect(app.find('models.*').map(b => b.key)).to.eql([
'models.ProductModel',
]);
});

it('injects component dependencies', () => {
class ConfigComponent {
providers = {
Expand Down
18 changes: 18 additions & 0 deletions packages/core/test/unit/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ describe('Application', () => {
}
});

describe('model binding', () => {
let app: Application;
class MyModel {}

beforeEach(givenApp);

it('binds a model', () => {
const binding = app.model(MyModel);
expect(Array.from(binding.tags)).to.containEql('model');
expect(binding.key).to.equal('models.MyModel');
expect(findKeysByTag(app, 'model')).to.containEql(binding.key);
});

function givenApp() {
app = new Application();
}
});

describe('component binding', () => {
let app: Application;
class MyController {}
Expand Down

0 comments on commit 9a4b6d8

Please sign in to comment.