lemo-crm/web/app/services/tool-service.js

49 lines
2.0 KiB
JavaScript

import Ember from 'ember';
import BaseService from './service';
import Ajax from './ajax';
import Store from './store';
export default Ember.Service.extend({
getServiceByRouteName: function(routeName) {
let me = this;
let container = Ember.getOwner(me);
let service = container.lookup('service:' + routeName);
console.debug(`No Route Service [${routeName}] Found, Try To Find Model Service`);
let indexTrimmedRouteName = null;
if (/\.index$/.test(routeName)) {
console.debug(`Route [${routeName}] Is Index, Try To Trim index Find Model Service`);
indexTrimmedRouteName = routeName.replace(/\.index$/, '');
service = container.lookup('service:' + indexTrimmedRouteName);
if (!service) {
console.debug(`Route [${indexTrimmedRouteName}] Is Trimed Index, But No Service Found Still.`);
service = container.lookup('service:' + indexTrimmedRouteName + '.service');
}
}
else {
service = container.lookup('service:' + routeName + '.service');
}
if (!service) {
let parentRouteName = (indexTrimmedRouteName || routeName).replace(/\.[^\.]+$/, '');
console.debug(`No Route Model Service [${routeName}.service] Found, Try To Find Parent Service [${parentRouteName}.service]`);
if (parentRouteName) {
console.debug(`Route [${parentRouteName}] Service Found`);
service = container.lookup('service:' + parentRouteName + '.service');
}
if (!service) {
console.info('No Service Found, Create Default');
let ajax = Ajax.create();
service = BaseService.create({
modelName: parentRouteName.match(/\.?([^\.]+)$/)[1],
store: Store.create({
ajax: ajax
}),
ajax: ajax
});
}
}
return service;
}
});