我的 angular2 (angular-cli) 应用程序中将有 400 多个模型用于 js-data。
我的项目结构是这样的:
- src/
- app/
- services/
- pipes/
- ui/
- data/
- store.ts
- models/
- model1.ts
- model2.ts
- ...
- model400.ts
在商店中,我需要导入,并将映射添加到商店。模型文件实际上只是 js-data 3 的映射器配置。
目前,它们看起来像这样:
// src/app/data/models/model1.ts
export default {
schema: {
name: 'model1',
properties: {
id: { type: 'integer' }
}
},
relations: {}
}
我的商店目前看起来像这样:
// src/app/data/store.ts
import {
DataStore,
Mapper,
Record,
Schema,
utils
} from 'js-data'
import {HttpAdapter} from 'js-data-http'
declare var require: any
export const adapter = new HttpAdapter({
// Our API sits behind the /api path
basePath: '/api'
});
export const store = new DataStore({});
store.registerAdapter('http', adapter, { default: true });
import { model1Config} from './models/model1';
import { model2Config } from './models/model2';
import { model3Config } from './models/model3';
// at this point, I give up, cause this is more tedious
// than cutting grass with a finger nail clipper
store.defineMapper('model1', model1Config);
store.defineMapper('model2', model2Config);
store.defineMapper('model3', model3Config);
如果无论如何要遍历模型文件夹中的每个文件,那就太好了。
angular-cli 应该最终将所有 ts/js 编译成一个 js 文件,所以我不需要担心任何无法在客户端运行的东西。(所以,我有西兰花,以及捆绑了其他任何构建工具,我只是不知道在这种情况下它们中的任何一个是否对我有用)