ember-source
Version:
A JavaScript framework for creating ambitious web applications
136 lines (108 loc) • 3.45 kB
JavaScript
import { a as createComputeRef, U as UNDEFINED_REFERENCE, v as valueForRef } from './reference-BshxG6wn.js';
import { f as reifyPositional } from './debug-render-tree-CF5O4-WI.js';
import { i as internalHelper } from './hash-Dkj3I4L4.js';
import { setPath, getPath } from '../@glimmer/global-context/index.js';
import { i as isDict } from './collections-C3Y8z_9v.js';
const isEmpty = value => {
return value === null || value === undefined || typeof value.toString !== 'function';
};
const normalizeTextValue = value => {
if (isEmpty(value)) {
return '';
}
return String(value);
};
/**
Concatenates the given arguments into a string.
Example:
```gjs
import { concat } from '@ember/helper';
<template>
{{yield (concat firstName " " lastName)}}
{{! would yield name="<first name value> <last name value>" to the component}}
</template>
```
or for angle bracket invocation, you actually don't need concat at all.
```handlebars
<SomeComponent @name="{{firstName}} {{lastName}}" />
```
@public
@method concat
*/
const concat = internalHelper(({
positional
}) => {
return createComputeRef(() => reifyPositional(positional).map(normalizeTextValue).join(''), null, 'concat');
});
/**
Dynamically look up a property on an object. The second argument to `{{get}}`
should have a string value, although it can be bound.
For example, these two usages are equivalent:
```app/components/developer-detail.gjs
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { get } from '@ember/object';
export default class extends Component {
@tracked developer = {
name: "Sandi Metz",
language: "Ruby"
}
<template>
{{this.developer.name}}
{{get this.developer "name"}}
</template>
}
```
If there were several facts about a person, the `{{get}}` helper can dynamically
pick one:
```app/templates/application.gjs
<template>
<DeveloperDetail @factName="language" />
</template>
```
```handlebars
{{get this.developer @factName}}
```
For a more complex example, this template would allow the user to switch
between showing the user's height and weight with a click:
```app/components/developer-detail.gjs
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { get } from '@ember/object';
export default class extends Component {
@tracked developer = {
name: "Sandi Metz",
language: "Ruby"
}
@tracked currentFact = 'name'
showFact = (fact) => {
this.currentFact = fact;
}
<template>
{{get this.developer this.currentFact}}
<button {{on 'click' (fn this.showFact "name")}}>Show name</button>
<button {{on 'click' (fn this.showFact "language")}}>Show language</button>
</template>
}
```
@public
@method get
*/
const get = internalHelper(({
positional
}) => {
let sourceRef = positional[0] ?? UNDEFINED_REFERENCE;
let pathRef = positional[1] ?? UNDEFINED_REFERENCE;
return createComputeRef(() => {
let source = valueForRef(sourceRef);
if (isDict(source)) {
return getPath(source, String(valueForRef(pathRef)));
}
}, value => {
let source = valueForRef(sourceRef);
if (isDict(source)) {
return setPath(source, String(valueForRef(pathRef)), value);
}
}, 'get');
});
export { concat as c, get as g };