Skip to content

Commit 74cbd94

Browse files
author
Robert Jackson
authored
link-to with conditional model (#158)
`link-to` with conditional model
2 parents 5fa0608 + 4fc4a70 commit 74cbd94

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

transforms/angle-brackets/test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,20 @@ test('link-to', () => {
338338
let input = `
339339
{{#link-to "about"}}About Us{{/link-to}}
340340
{{#link-to this.dynamicRoute}}About Us{{/link-to}}
341+
{{#link-to "user" this.first this.second}}Show{{/link-to}}
342+
{{#link-to "user" this.first this.second (query-params foo="baz")}}Show{{/link-to}}
343+
{{#link-to "user" this.first}}Show{{/link-to}}
344+
{{#link-to "user" this.first (query-params foo="baz")}}Show{{/link-to}}
341345
`;
342346

343347
expect(runTest('link-to.hbs', input)).toMatchInlineSnapshot(`
344348
"
345349
<LinkTo @route=\\"about\\">About Us</LinkTo>
346350
<LinkTo @route={{this.dynamicRoute}}>About Us</LinkTo>
351+
<LinkTo @route=\\"user\\" @models={{array this.first this.second}}>Show</LinkTo>
352+
<LinkTo @route=\\"user\\" @models={{array this.first this.second}} @query={{hash foo=\\"baz\\"}}>Show</LinkTo>
353+
<LinkTo @route=\\"user\\" @model={{this.first}}>Show</LinkTo>
354+
<LinkTo @route=\\"user\\" @model={{this.first}} @query={{hash foo=\\"baz\\"}}>Show</LinkTo>
347355
"
348356
`);
349357
});
@@ -374,6 +382,11 @@ test('link-to-inline', () => {
374382
class="t__em-link"
375383
}}
376384
{{link-to (t "show") "flight" event.flight.id class="btn btn-default btn-sm pull-right"}}
385+
{{link-to (t "show") "user" (if linkActor event.actor.id event.user.id)}}
386+
{{link-to "Show" "user" this.first this.second}}
387+
{{link-to "Show" "user" this.first this.second (query-params foo="baz")}}
388+
{{link-to "Show" "user" this.first}}
389+
{{link-to "Show" "user" this.first (query-params foo="baz")}}
377390
`;
378391

379392
/**
@@ -388,6 +401,11 @@ test('link-to-inline', () => {
388401
<LinkTo @route={{this.dynamicPath}} class=\\"tabs__discrete-tab\\" @activeClass=\\"o__selected\\" @current-when=\\"apps.segments\\" data-test-segment-link=\\"segments\\">Segments</LinkTo>
389402
<LinkTo @route=\\"apps.app.companies.segments.segment\\" @model={{segment}} class=\\"t__em-link\\">{{segment.name}}</LinkTo>
390403
<LinkTo @route=\\"flight\\" @model={{event.flight.id}} class=\\"btn btn-default btn-sm pull-right\\">{{t \\"show\\"}}</LinkTo>
404+
<LinkTo @route=\\"user\\" @model={{if linkActor event.actor.id event.user.id}}>{{t \\"show\\"}}</LinkTo>
405+
<LinkTo @route=\\"user\\" @models={{array this.first this.second}}>Show</LinkTo>
406+
<LinkTo @route=\\"user\\" @models={{array this.first this.second}} @query={{hash foo=\\"baz\\"}}>Show</LinkTo>
407+
<LinkTo @route=\\"user\\" @model={{this.first}}>Show</LinkTo>
408+
<LinkTo @route=\\"user\\" @model={{this.first}} @query={{hash foo=\\"baz\\"}}>Show</LinkTo>
391409
"
392410
`);
393411
});

transforms/angle-brackets/transform.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,19 @@ function transformToAngleBracket(env, fileInfo, config) {
240240
// eslint-disable-next-line no-unused-vars
241241
let [_, secondParamInput] = params;
242242
if (secondParamInput.type === 'SubExpression') {
243-
let _queryParam = b.attr('@query', b.mustache(b.path('hash'), [], secondParamInput.hash));
244-
attributes = [firstParamOutput, _queryParam];
243+
let _queryParamOrModel;
244+
if (isQueryParam(secondParamInput)) {
245+
_queryParamOrModel = b.attr(
246+
'@query',
247+
b.mustache(b.path('hash'), [], secondParamInput.hash)
248+
);
249+
} else {
250+
_queryParamOrModel = b.attr(
251+
'@model',
252+
b.mustache(secondParamInput.path, secondParamInput.params)
253+
);
254+
}
255+
attributes = [firstParamOutput, _queryParamOrModel];
245256
} else {
246257
let _modelParam = b.attr('@model', transformModelParams(secondParamInput));
247258
attributes = [firstParamOutput, _modelParam];
@@ -255,8 +266,15 @@ function transformToAngleBracket(env, fileInfo, config) {
255266
let _qpParam;
256267

257268
if (hasQueryParamHelper) {
258-
_modelsParam = b.attr('@model', transformModelParams(models[0]));
259-
_qpParam = b.attr('@query', b.mustache(b.path('hash'), [], models[1].hash));
269+
if (models.length < 3) {
270+
_modelsParam = b.attr('@model', transformModelParams(models[0]));
271+
} else {
272+
_modelsParam = b.attr(
273+
'@models',
274+
b.mustache(b.path('array'), models.slice().splice(0, 2))
275+
);
276+
}
277+
_qpParam = b.attr('@query', b.mustache(b.path('hash'), [], models[models.length - 1].hash));
260278
} else {
261279
_modelsParam = b.attr('@models', b.mustache(b.path('array'), models));
262280
}

0 commit comments

Comments
 (0)