Skip to content

Commit fb91c8a

Browse files
authored
Deprecate {{partial}} (#18491)
Deprecate `{{partial}}`
2 parents 74d3095 + ff9e96b commit fb91c8a

File tree

9 files changed

+224
-164
lines changed

9 files changed

+224
-164
lines changed

packages/@ember/-internals/glimmer/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@
340340
@method partial
341341
@for Ember.Templates.helpers
342342
@param {String} partialName The name of the template to render minus the leading underscore.
343+
@deprecated Use a component instead
343344
@public
344345
*/
345346

packages/@ember/-internals/glimmer/lib/resolver.ts

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { privatize as P } from '@ember/-internals/container';
22
import { ENV } from '@ember/-internals/environment';
33
import { Factory, FactoryClass, LookupOptions, Owner } from '@ember/-internals/owner';
4-
import { lookupPartial, OwnedTemplateMeta } from '@ember/-internals/views';
4+
import { OwnedTemplateMeta } from '@ember/-internals/views';
55
import {
66
EMBER_GLIMMER_SET_COMPONENT_TEMPLATE,
77
EMBER_MODULE_UNIFICATION,
88
} from '@ember/canary-features';
99
import { isTemplateOnlyComponent } from '@ember/component/template-only';
10-
import { assert } from '@ember/debug';
10+
import { assert, deprecate } from '@ember/debug';
11+
import { PARTIALS } from '@ember/deprecated-features';
12+
import EmberError from '@ember/error';
1113
import { _instrumentStart } from '@ember/instrumentation';
1214
import {
1315
ComponentDefinition,
@@ -176,6 +178,62 @@ function lookupComponent(owner: Owner, name: string, options: LookupOptions): Op
176178
return lookupComponentPair(owner, name);
177179
}
178180

181+
let lookupPartial: { templateName: string; owner: Owner } | any;
182+
let templateFor: { owner: Owner; underscored: string; name: string } | any;
183+
let parseUnderscoredName: { templateName: string } | any;
184+
185+
if (PARTIALS) {
186+
lookupPartial = function(templateName: string, owner: Owner) {
187+
deprecate(
188+
`The use of \`{{partial}}\` is deprecated, please refactor the "${templateName}" partial to a component`,
189+
false,
190+
{
191+
id: 'ember-views.partial',
192+
until: '4.0.0',
193+
url: 'https://deprecations.emberjs.com/v3.x#toc_ember-views-partial',
194+
}
195+
);
196+
197+
if (templateName === null) {
198+
return;
199+
}
200+
201+
let template = templateFor(owner, parseUnderscoredName(templateName), templateName);
202+
203+
assert(`Unable to find partial with name "${templateName}"`, Boolean(template));
204+
205+
return template;
206+
};
207+
208+
templateFor = function(owner: any, underscored: string, name: string) {
209+
if (PARTIALS) {
210+
if (!name) {
211+
return;
212+
}
213+
assert(`templateNames are not allowed to contain periods: ${name}`, name.indexOf('.') === -1);
214+
215+
if (!owner) {
216+
throw new EmberError(
217+
'Container was not found when looking up a views template. ' +
218+
'This is most likely due to manually instantiating an Ember.View. ' +
219+
'See: http://git.io/EKPpnA'
220+
);
221+
}
222+
223+
return owner.lookup(`template:${underscored}`) || owner.lookup(`template:${name}`);
224+
}
225+
};
226+
227+
parseUnderscoredName = function(templateName: string) {
228+
let nameParts = templateName.split('/');
229+
let lastPart = nameParts[nameParts.length - 1];
230+
231+
nameParts[nameParts.length - 1] = `_${lastPart}`;
232+
233+
return nameParts.join('/');
234+
};
235+
}
236+
179237
interface IBuiltInHelpers {
180238
[name: string]: Helper | undefined;
181239
}
@@ -306,8 +364,12 @@ export default class RuntimeResolver implements IRuntimeResolver<OwnedTemplateMe
306364
* Called by CompileTimeLookup to lookup partial
307365
*/
308366
lookupPartial(name: string, meta: OwnedTemplateMeta): Option<number> {
309-
let partial = this._lookupPartial(name, meta);
310-
return this.handle(partial);
367+
if (PARTIALS) {
368+
let partial = this._lookupPartial(name, meta);
369+
return this.handle(partial);
370+
} else {
371+
return null;
372+
}
311373
}
312374

313375
// end CompileTimeLookup

packages/@ember/-internals/glimmer/tests/integration/application/engine-test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ moduleFor(
251251
}
252252

253253
['@test attrs in an engine']() {
254+
expectDeprecation(
255+
`The use of \`{{partial}}\` is deprecated, please refactor the "troll" partial to a component`
256+
);
257+
254258
this.setupEngineWithAttrs([]);
255259

256260
return this.visit('/').then(() => {
@@ -431,7 +435,11 @@ moduleFor(
431435
}
432436

433437
['@test visit() with partials in routable engine'](assert) {
434-
assert.expect(2);
438+
assert.expect(3);
439+
440+
expectDeprecation(
441+
`The use of \`{{partial}}\` is deprecated, please refactor the "foo" partial to a component`
442+
);
435443

436444
let hooks = [];
437445

@@ -449,7 +457,11 @@ moduleFor(
449457
}
450458

451459
['@test visit() with partials in non-routable engine'](assert) {
452-
assert.expect(2);
460+
assert.expect(3);
461+
462+
expectDeprecation(
463+
`The use of \`{{partial}}\` is deprecated, please refactor the "foo" partial to a component`
464+
);
453465

454466
let hooks = [];
455467

packages/@ember/-internals/glimmer/tests/integration/components/curly-components-test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,9 @@ moduleFor(
760760
template: '{{partial "partialWithYield"}} - In component',
761761
});
762762

763-
this.render('{{#foo-bar}}hello{{/foo-bar}}');
763+
expectDeprecation(() => {
764+
this.render('{{#foo-bar}}hello{{/foo-bar}}');
765+
}, 'The use of `{{partial}}` is deprecated, please refactor the "partialWithYield" partial to a component');
764766

765767
this.assertComponentElement(this.firstChild, {
766768
content: 'yielded: [hello] - In component',
@@ -780,7 +782,9 @@ moduleFor(
780782
template: '{{partial "partialWithYield"}} - In component',
781783
});
782784

783-
this.render('{{#foo-bar as |value|}}{{value}}{{/foo-bar}}');
785+
expectDeprecation(() => {
786+
this.render('{{#foo-bar as |value|}}{{value}}{{/foo-bar}}');
787+
}, 'The use of `{{partial}}` is deprecated, please refactor the "partialWithYield" partial to a component');
784788

785789
this.assertComponentElement(this.firstChild, {
786790
content: 'yielded: [hello] - In component',

0 commit comments

Comments
 (0)