yoqw/web/cms/app/services/tool-service.js
2020-07-01 15:22:07 +08:00

41 lines
1.4 KiB
JavaScript

import { getOwner } from '@ember/application';
import Service from '@ember/service';
import BaseService from './service';
export default Service.extend({
getServiceByRouteName: function(routeName) {
const me = this;
let container = getOwner(me);
let service = container.lookup('service:' + routeName);
let indexTrimmedRouteName = null;
if (/\.index$/.test(routeName)) {
indexTrimmedRouteName = routeName.replace(/\.index$/, '');
service = container.lookup('service:' + indexTrimmedRouteName);
if (!service) {
service = container.lookup('service:' + indexTrimmedRouteName + '.service');
}
}
else {
service = container.lookup('service:' + routeName + '.service');
}
if (!service) {
let parentRouteName = (indexTrimmedRouteName || routeName).replace(/\.[^.]+$/, '');
if (parentRouteName) {
service = container.lookup('service:' + parentRouteName + '.service');
}
if (!service) {
service = BaseService.create({
modelName: parentRouteName.match(/\.?([^.]+)$/)[1],
store: me.get('store'),
ajax: me.get('ajax'),
message: me.get('message'),
dialog: me.get('dialog')
});
}
}
return service;
}
});