Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add refresh method #84

Merged
merged 9 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,13 @@ node_modules/
# misc
npm-debug.log*
yarn-error.log
.DS_Store
/.DS_Store

# yarn
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
18 changes: 18 additions & 0 deletions ember-engines-router-service/src/services/engine-router-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ export default class EngineRouterService extends Service.extend(Evented) {
return this._externalRoutes[externalRouteName];
}

refresh(routeName = this.currentRouteName, ...args) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this approach might be able to solve #74 as well @SergeAstapov if you want I can add it as well

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aklkv sure, feel free to open separate PR, to keep each PR focused on one thing

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @SergeAstapov here, let's open another PR for this purpose

if (resemblesURL(routeName)) {
return this.externalRouter.refresh(routeName);
}

return this.externalRouter.refresh(
namespaceEngineRouteName(this._mountPoint, routeName),
...args
);
}

refreshExternal(routeName, ...args) {
return this.externalRouter.refresh(
this.getExternalRouteName(routeName),
...args
);
aklkv marked this conversation as resolved.
Show resolved Hide resolved
}

transitionTo(routeName, ...args) {
if (resemblesURL(routeName)) {
return this.externalRouter.transitionTo(routeName);
Expand Down
10 changes: 6 additions & 4 deletions ember-engines-router-service/types/services/router.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type RouterService from '@ember/routing/router-service';

export default interface EnginesRouterService extends Omit<
RouterService,
"currentRoute" | "recognize" | "recognizeAndLoad"
> {
export default interface EnginesRouterService
extends Omit<
RouterService,
'currentRoute' | 'recognize' | 'recognizeAndLoad'
> {
isActiveExternal: RouterService['isActive'];
replaceWithExternal: RouterService['replaceWith'];
transitionToExternal: RouterService['transitionTo'];
refreshExternal: RouterService['refresh'];
urlForExternal: RouterService['urlFor'];
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"release": "release-it"
},
"devDependencies": {
"release-it": "^15.5.1",
"@release-it-plugins/lerna-changelog": "^5.0.0",
"@release-it-plugins/workspaces": "^3.2.0"
"@release-it-plugins/workspaces": "^3.2.0",
"release-it": "^15.5.1"
},
"publishConfig": {
"registry": "https://registry.npmjs.org"
Expand Down
11 changes: 11 additions & 0 deletions test-app/app/routes/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Route from '@ember/routing/route';

let count = 0;

export default class extends Route {
model() {
return {
count: count++,
};
}
}
3 changes: 3 additions & 0 deletions test-app/app/templates/application.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<h1>Ember Engines Demo</h1>
<span class='global-refresh-counter'>
aklkv marked this conversation as resolved.
Show resolved Hide resolved
{{@model.count}}
</span>

<LinkTo @route="routeless-engine-demo" class="routeless-engine">Routeless Engine</LinkTo> |
<LinkTo @route="routable-engine-demo" class="routeable-engine">Routeable Engine</LinkTo> |
Expand Down
2 changes: 1 addition & 1 deletion test-app/lib/eager-blog/addon/templates/application.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h3 class="title">Eager engine</h3>

<LinkTo @route="index" @current-when="index post" class="current-when-test-link">All Posts</LinkTo> |
<LinkTo @route="post">Post 1</LinkTo>
<LinkTo @route="post" @model={{1}}>Post 1</LinkTo>

{{outlet}}
31 changes: 29 additions & 2 deletions test-app/lib/ember-blog/addon/components/hello-name.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
<div class="hello-name" ...attributes>
Hello, {{@name}}!
<div class='hello-name' ...attributes>
<span class='greeting'>
Hello,
{{@name}}!
</span>

<button
class='refresh'
type='button'
{{on 'click' this.refresh}}
>
Refresh
</button>

<button
class='refresh-route'
type='button'
{{on 'click' this.refreshRoute}}
>
Refresh Route
</button>

<button
class='refresh-external'
type='button'
{{on 'click' this.refreshExternal}}
>
Refresh External
</button>
</div>
26 changes: 25 additions & 1 deletion test-app/lib/ember-blog/addon/components/hello-name.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { later, cancel } from '@ember/runloop';
import Component from '@glimmer/component';
import { later, cancel } from '@ember/runloop';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';

export default class extends Component {
@service router;

@tracked name = this.args.name;
@tracked clickCount = 0;

constructor() {
super(...arguments);
Expand All @@ -20,4 +25,23 @@ export default class extends Component {
cancel(this._later);
super.willDestroy(...arguments);
}

@action click() {
this.clickCount++;
}

@action
refresh() {
this.router.refresh();
}

@action
refreshRoute() {
this.router.refresh('new');
}

@action
refreshExternal() {
this.router.refreshExternal('home');
}
}
19 changes: 19 additions & 0 deletions test-app/lib/ember-blog/addon/routes/new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { inject as service } from '@ember/service';
import Route from '@ember/routing/route';

let count = 0;
aklkv marked this conversation as resolved.
Show resolved Hide resolved

export default class extends Route {
@service exampleService;

model() {
// cause a service to be instantiated, so that our tests can
// confirm that it gets cleaned up
this.exampleService;

return {
name: 'Derek Zoolander',
count: count++,
};
}
}
4 changes: 4 additions & 0 deletions test-app/lib/ember-blog/addon/templates/new.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<div class="engine">
New Route!!

<span class="route-refresh-counter">
{{@model.count}}
</span>

<HelloWorld class="routable-hello-world" />

<button class="trigger-transition-to" type="button" {{on "click" this.goAway}}>Go Away!</button>
Expand Down
2 changes: 1 addition & 1 deletion test-app/lib/ember-blog/addon/templates/post.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</button>

<button class="routable-replace-external-button {{if this.replaceWithExternal "replaced-with-external"}}" type="button" {{on "click" this.replaceWithHomeByService}}>
Go home progammatically (Engine Router Service)
Go home programmatically (Engine Router Service)
</button>


Expand Down
3 changes: 3 additions & 0 deletions test-app/lib/ember-chat/addon/components/spanish-greeting.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<span class="greeting">Hola</span> from {{@name}}!
<button class="clicker" type="button" {{on "click" this.click}}>Clicker</button>
<span class="click-count">{{this.clickCount}}</span>
11 changes: 11 additions & 0 deletions test-app/lib/ember-chat/addon/components/spanish-greeting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';

export default class extends Component {
@tracked clickCount = 0;

@action click() {
this.clickCount++;
}
}
1 change: 1 addition & 0 deletions test-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"devDependencies": {
"@babel/core": "^7.20.7",
"@babel/eslint-parser": "^7.19.1",
"@ember/legacy-built-in-components": "0.4.1",
villander marked this conversation as resolved.
Show resolved Hide resolved
"@ember/optional-features": "^2.0.0",
"@ember/test-helpers": "^2.9.3",
"@embroider/test-setup": "^2.0.2",
Expand Down
48 changes: 48 additions & 0 deletions test-app/tests/acceptance/routeable-engine-demo-refresh-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { module, test } from 'qunit';
aklkv marked this conversation as resolved.
Show resolved Hide resolved
import { setupApplicationTest } from 'ember-qunit';
import { visit, find, click } from '@ember/test-helpers';

module('Acceptance | routeless engine demo', function (hooks) {
setupApplicationTest(hooks);

module('Engine Router Service Refresh Method', function () {
test('refresh without params triggers refresh with current route', async function (assert) {
await visit('/routable-engine-demo/ember-blog/new');

let counter = await find('.route-refresh-counter').textContent;
assert.dom('.route-refresh-counter').hasText(counter);
aklkv marked this conversation as resolved.
Show resolved Hide resolved
await click('.refresh');

counter = parseInt(counter, 10);
counter = ++counter;
counter = counter.toString();
assert.dom('.route-refresh-counter').hasText(counter);
});

test('refresh with params triggers refresh on provided route', async function (assert) {
await visit('/routable-engine-demo/ember-blog/new');

let counter = await find('.route-refresh-counter').textContent;
assert.dom('.route-refresh-counter').hasText(counter);
await click('.refresh-route');

counter = parseInt(counter, 10);
counter = ++counter;
counter = counter.toString();
assert.dom('.route-refresh-counter').hasText(counter);
});

test('refresh external route', async function (assert) {
await visit('/routable-engine-demo/ember-blog/new');

let counter = await find('.route-refresh-counter').textContent;
assert.dom('.global-refresh-counter').hasText(counter);
await click('.refresh-external');

counter = parseInt(counter, 10);
counter = ++counter;
counter = counter.toString();
assert.dom('.global-refresh-counter').hasText(counter);
});
});
});
Loading