This addon was created as a rethinking of ember-ref-modifier, with a more simplified API and without some of the downsides of the previous implementation.
The addon allows users to get access to DOM nodes inside components, including accessing wrapping/destroying logic.
A simple use case:
ref
modifier with passed name to an element.import Component from '@glimmer/component';
import { ref } from 'ember-ref-bucket';
export default class MyComponent extends Component {
@ref("FavouriteNode") node;
// this.node === "<div>hello</div>"
}
API differences, comparing to ember-ref-modifier
:
In ember-ref-modifier
ref modifier accept 2 positional arguments {{ref this "property"}}
:
this
)"property"
)In ember-ref-bucket
ref modifier accept 1 positional argument {{create-ref "field"}}
:
"field"
)reference name should be passed as an argument to the @ref("field")
decorator, to allow it to find the reference by name.
ember install ember-ref-bucket
import Component from '@glimmer/component';
import { ref } from 'ember-ref-bucket';
import { action } from '@ember/object';
export class Player extends Component {
@ref('player') audioNode;
@action onPlay() {
this.audioNode.play()
}
}
div
to node
property.import Component from '@glimmer/component';
import { ref } from 'ember-ref-bucket';
export default class MyComponent extends Component {
@ref("field") node = null;
}
div
content updatesdiv
as component argumentregisterNodeDestructor
This method is very useful if you want to wrap the node and control its lifecycle.
import Component from '@glimmer/component';
import { ref, registerNodeDestructor } from 'ember-ref-bucket';
class NodeWrapper {
constructor(node) {
this.node = node;
}
destroy() {
this.node = null;
}
value() {
return this.node.textContent;
}
}
export default class WrappedNodeComponent extends Component {
@ref('field', (node) => {
const instance = new NodeWrapper(node);
registerNodeDestructor(node, () => instance.destroy());
return instance;
}) node = null;
get value() {
return this.node?.value();
}
}
import { ref, globalRef, trackedRef, trackedGlobalRef } from 'ember-ref-bucket';
/*
ref - usage: @ref('foo', nodeWrapFn?), ref to bucket with current component context
globalRef - usage: @globalRef('foo', nodeWrapFn?), ref to global context (app)
trackedRef - usage: @trackedRef('foo', nodeWrapFn?), tracked ref to local context
trackedGlobalRef - usage: @trackedGlobalRef('foo', nodeWrapFn?), tracked ref to global context (app)
*/
import { registerNodeDestructor, unregisterNodeDestructor } from 'ember-ref-bucket';
/*
registerNodeDestructor(node, fn) - to assign any ref-node destructor
unregisterNodeDestructor(node, fn) - to remove assigned ref-node destructor
usage will be like:
@ref('field', (node) => {
const item = new InputMask(node);
registerNodeDestructor(node, () => item.destroy());
return item;
});
*/
/*
nodeFor - functional low-level primitive to get node access
*/
import { nodeFor } from 'ember-ref-bucket';
const domNode = nodeFor(this, 'field');
@trackedRef
decoratorsIf you use dom node in @tracked
chain calculations, you should use trackedRef
.
If you don't need to rerun the tracked chain (for example, you use ref
only for some event-based dom access), you should not use trackedRef
.
{{create-tracked-ref}}
modifierscreate-tracked-ref
modifier. It can add observe resizing and mutations for the associated element and will mark it as "dirty" for any mutation.Options:
resize
- default: false
, if truthy observes the resizing of the DOM element.attributes
- default: false
, if truthy observes the changing of any attribute on the DOM element.characterdata
- default: false
, if truthy observes the change of the innerText of the DOM element. Note that setting innerText can change the childlist or the characterdata depending on the current content of the element.childlist
- default: false
, if truthy observes changes to the list of direct children of the DOM element.subtree
- default: false
, if truthy observes the above options on the entire DOM subtree, not just the element decorated by the modifier.{{tracked-ref-to}}
helperstracked-ref-to
helper.ref-to
helper.create-ref
modifier and ref-to
helpers will not work in template-only components (because of no context). You should use create-global-ref
and global-ref-to
instead. You can also provide a bucket
param to the create-ref
modifier / helper.The addon provide only 1 modifier (create-ref
) and 1 helper (ref-to
). Other derivatives will be transformed, and are described below:
Invocation | Will be transformed to |
---|---|
{{create-ref "foo"}} |
{{create-ref "foo" bucket=this}} |
{{create-tracked-ref "foo"}} |
{{create-ref "foo" bucket=this tracked=true}} |
{{create-global-ref "foo"}} |
{{create-ref "foo" bucket=undefined}} |
{{create-tracked-global-ref "foo"}} |
{{create-ref "foo" bucket=undefined tracked=true}} |
Invocation | Will be transformed to |
---|---|
{{ref-to "foo"}} |
{{ref-to "foo" bucket=this}} |
{{tracked-ref-to "foo"}} |
{{ref-to "foo" bucket=this tracked=true}} |
{{global-ref-to "foo"}} |
{{ref-to "foo" bucket=undefined}} |
{{tracked-global-ref-to "foo"}} |
{{ref-to "foo" bucket=undefined tracked=true}} |
See the Contributing guide for details.
This project is licensed under the MIT License.
ember-ref-modifier Check ember-ref-bucket first! ember-ref-modifier likely will be deprecated after ember-ref-bucket major release An implementation of the {{ref}} element modifier.Heavily inspired by
描述 (Description) 如果EXPR或$ _(如果未提供EXPR)是引用,则此函数返回true值。 返回的实际值还定义了引用引用的实体类型。 内置类型是 - REF SCALAR ARRAY HASH CODE GLOB LVALUE IO::Handle 如果变量被bless()函数祝福,那么将返回新的数据类型。 新数据类型通常是类名。 语法 (Syntax) 以下是此函数的简单语法
这用于创建参考值。 创建参考值时,可以选择提供验证器功能,该功能将验证创建的值。 语法 (Syntax) 以下是语法。 (ref x options) Parameters - 'x'是需要提供给参考的值。 “选项”是一组可以提供的选项,例如validate命令。 Return Value - 引用及其对应值。 例子 (Example) 以下程序显示了如何使用它的示例。 (ns clojure.
我对指针在锈迹中的工作原理有点困惑。有,,
预期:string ref 被用来给元素或子组件注册引用信息。引用信息将会注册在父组件的 $refs 对象上。如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素;如果用在子组件上,引用就指向组件实例: <!-- `vm.$refs.p` will be the DOM node --> <p ref="p">hello</p> <!-- `vm.$refs.child` will
此函数用于设置对新值的引用值,而不管旧值是什么。 语法 (Syntax) 以下是语法。 (ref-set refname newvalue) Parameters - 'refname'是保存参考值的变量的名称。 'newvalue'是需要与引用类型关联的新值。 Return Value - 引用及其对应的新值。 例子 (Example) 以下程序显示了如何使用它的示例。 (ns clojure