Пример #1
0
QUnit.test("validating resolved objects", function() {
  let types = ['route', 'component', 'view', 'service'];

  // Valid setup
  application.FooRoute = Route.extend();
  application.FooComponent = Component.extend();
  application.FooView = View.extend();
  application.FooService = Service.extend();

  forEach(types, function(type) {
    // No errors when resolving correct object types
    registry.resolve(`${type}:foo`);

    // Unregister to clear cache
    registry.unregister(`${type}:foo`);
  });

  // Invalid setup
  application.FooRoute = Component.extend();
  application.FooComponent = View.extend();
  application.FooView = Service.extend();
  application.FooService = Route.extend();

  forEach(types, function(type) {
    let matcher = new RegExp(`to resolve to an Ember.${capitalize(type)}`);
    expectAssertion(function() {
      registry.resolve(`${type}:foo`);
    }, matcher, `Should assert for ${type}`);
  });
});
 boot(function() {
   appInstance.register('service:name-builder', Service.extend({
     build() {
       serviceCalled = true;
     }
   }));
   appInstance.register('helper:full-name', Helper.extend({
     nameBuilder: inject.service('name-builder'),
     compute() {
       this.get('nameBuilder').build();
     }
   }));
 });
Пример #3
0
QUnit.test('services can be injected into routes', function() {
  let owner = buildOwner();

  owner.register('route:application', EmberRoute.extend({
    authService: inject.service('auth')
  }));

  owner.register('service:auth', Service.extend());

  let appRoute = owner.lookup('route:application');
  let authService = owner.lookup('service:auth');

  equal(authService, appRoute.get('authService'), 'service.auth is injected');
});
Пример #4
0
  test("services can be injected into components", function() {
    var container = new Container();

    container.register('component:application', Component.extend({
      profilerService: inject.service('profiler')
    }));

    container.register('service:profiler', Service.extend());

    var appComponent = container.lookup('component:application'),
    profilerService = container.lookup('service:profiler');

    equal(profilerService, appComponent.get('profilerService'), "service.profiler is injected");
  });
Пример #5
0
QUnit.test('services can be injected into components', function() {
  let owner = buildOwner();

  owner.register('component:application', Component.extend({
    profilerService: inject.service('profiler')
  }));

  owner.register('service:profiler', Service.extend());

  var appComponent = owner.lookup('component:application');
  var profilerService = owner.lookup('service:profiler');

  equal(profilerService, appComponent.get('profilerService'), 'service.profiler is injected');
});
Пример #6
0
  test("services can be injected into controllers", function() {
    var container = new Container();

    container.register('controller:application', Controller.extend({
      authService: inject.service('auth')
    }));

    container.register('service:auth', Service.extend());

    var appController = container.lookup('controller:application'),
      authService = container.lookup('service:auth');

    equal(authService, appController.get('authService'), "service.auth is injected");
  });
Пример #7
0
QUnit.test('services can be injected into controllers', function() {
  let owner = buildOwner();

  owner.register('controller:application', Controller.extend({
    authService: inject.service('auth')
  }));

  owner.register('service:auth', Service.extend());

  let appController = owner.lookup('controller:application');
  let authService = owner.lookup('service:auth');

  equal(authService, appController.get('authService'), 'service.auth is injected');
});
Пример #8
0
QUnit.test("services can be injected into views", function() {
  var registry = new Registry();
  var container = registry.container();

  registry.register('view:application', View.extend({
    profilerService: inject.service('profiler')
  }));

  registry.register('service:profiler', Service.extend());

  var appView = container.lookup('view:application');
  var profilerService = container.lookup('service:profiler');

  equal(profilerService, appView.get('profilerService'), "service.profiler is injected");
});
Пример #9
0
QUnit.test('services can be injected into components', function() {
  var registry = new Registry();
  var container = registry.container();

  registry.register('component:application', Component.extend({
    profilerService: inject.service('profiler')
  }));

  registry.register('service:profiler', Service.extend());

  var appComponent = container.lookup('component:application');
  var profilerService = container.lookup('service:profiler');

  equal(profilerService, appComponent.get('profilerService'), 'service.profiler is injected');
});
Пример #10
0
  test("services can be injected into routes", function() {
    var registry = new Registry();
    var container = registry.container();

    registry.register('route:application', EmberRoute.extend({
      authService: inject.service('auth')
    }));

    registry.register('service:auth', Service.extend());

    var appRoute = container.lookup('route:application');
    var authService = container.lookup('service:auth');

    equal(authService, appRoute.get('authService'), "service.auth is injected");
  });
Пример #11
0
QUnit.test('no deprecation warning for service factories that extend from Ember.Service', function() {
  expectNoDeprecation();
  application.FooService = Service.extend();
  registry.resolve('service:foo');
});
Пример #12
0
export default Service.extend({
  router: null,

  targetState: readOnly('router.targetState'),
  currentState: readOnly('router.currentState'),
  currentRouteName: readOnly('router.currentRouteName'),
  currentPath: readOnly('router.currentPath'),

  availableRoutes() {
    return Object.keys(get(this, 'router').router.recognizer.names);
  },

  hasRoute(routeName) {
    return get(this, 'router').hasRoute(routeName);
  },

  transitionTo(routeName, models, queryParams, shouldReplace) {
    var router = get(this, 'router');

    var transition = router._doTransition(routeName, models, queryParams);

    if (shouldReplace) {
      transition.method('replace');
    }
  },

  normalizeQueryParams(routeName, models, queryParams) {
    var router = get(this, 'router');
    router._prepareQueryParams(routeName, models, queryParams);
  },

  generateURL(routeName, models, queryParams) {
    var router = get(this, 'router');
    if (!router.router) { return; }

    var visibleQueryParams = {};
    assign(visibleQueryParams, queryParams);

    this.normalizeQueryParams(routeName, models, visibleQueryParams);

    var args = routeArgs(routeName, models, visibleQueryParams);
    return router.generate.apply(router, args);
  },

  isActiveForRoute(contexts, queryParams, routeName, routerState, isCurrentWhenSpecified) {
    var router = get(this, 'router');

    var handlers = router.router.recognizer.handlersFor(routeName);
    var leafName = handlers[handlers.length - 1].handler;
    var maximumContexts = numberOfContextsAcceptedByHandler(routeName, handlers);

    // NOTE: any ugliness in the calculation of activeness is largely
    // due to the fact that we support automatic normalizing of
    // `resource` -> `resource.index`, even though there might be
    // dynamic segments / query params defined on `resource.index`
    // which complicates (and makes somewhat ambiguous) the calculation
    // of activeness for links that link to `resource` instead of
    // directly to `resource.index`.

    // if we don't have enough contexts revert back to full route name
    // this is because the leaf route will use one of the contexts
    if (contexts.length > maximumContexts) {
      routeName = leafName;
    }

    return routerState.isActiveIntent(routeName, contexts, queryParams, !isCurrentWhenSpecified);
  }
});