We know that under the component system, components must be nestable tree relationships. Let’s do some explanation from a piece of code below. In the code below, AddForm internally uses two custom components: ui-calendar and ui-timepicker.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<!-- Template --> <divclass="form"> <inputtype="text"class="form-title"placeholder="标题"value="{= title =}"> <textareaclass="form-desc"placeholder="备注"value="{= desc =}"></textarea>
submit: function () { this.ref('endDate') this.ref('endHour') } });
components
Components typically use other components by declaring custom elements.
Which subcomponent types can be used by the component view, which must first be specified by the component’s components member. In components object, the key is the name of a custom element, and the value is component class.
tips:Given the independence of components, San does not provide a way to register global components. Components must declare which components they use internally in their own components.
Some components may use themselves in content, such as tree nodes. We can set the value of this item in components to a string self
1 2 3 4 5 6 7
varNode = san.defineComponent({ // template
components: { 'ui-node': 'self' } });
owner and parent
The concept of owner and parent has been clarified by react, but it is still specific here.
owner of a component is whoever creates the one and manages its lifetime, interactive communication, etc. owner must be a component. owner of ui-calendar is component AddForm.
parent of a component is whoever would be the containing ancestor of the DOM hierarchy in view. parent of ui-calendar is the outer div。parent has no direct meaning to component management.
ref
When declaring a subcomponent with a name specified by s-ref, you can call it from the ref method of the owner component instance.
tips: With declarative initialization, data binding, and event binding, we rarely need to get an instance of a subcomponent in the owner component. Although San provides this approach, when you use it, please think about whether you want to do this.
message
With the dispatch method, components can dispatch messages to the upper layers of the component tree.
The message will be passed up the component tree until it encounters the first component that processes the message. We can declare messages to be processed by the component in messages. messages is an object, the key is a message’s name, and the value is a function of message handler that receives a parameter object containing the target (the component that dispatches the message) and the value (the value of the message).
// Declare messages that the component will process messages: { 'UI:select-item-selected': function (arg) { var value = arg.value; this.data.set('value', value);
// arg.target Get the component that dispatches the message } } });
Messages are primarily used for components to communicate with upper-level components of non-owner. For example, the owner of the component SelectItem in the slot is the upper-level component. And it needs to communicate with Select.
In some scenarios, we want components to not create subcomponents when their own views are rendered, but rather to have the flexibility to create subcomponents at some point in the future. For example
It is inconvenient using declarative way while parent of floating layer(subcomponent) is not in its root element el.
The list needs to be created and displayed only when the user clicks
Be careful to create subcomponents manually. Here are some tips to note, and the following code fragment also gives some clarification.
Manually created subcomponents don’t have to be declared in components
Avoid recreation. A simple approach is to keep a reference to the created subcomponents instance and check if it’s already exists before creating.
// Binding data and events of a manually created subcomponent to its owner via `owner` and `source` options. // 3.7.0+ varPerson = san.defineComponent({ template: '<div>' + ' <input type="text" value="{=name=}">' + ' <input type="text" value="{=email=}">' + ' <button on-click="done">Done</button>' + '</div>',
// 2-way data binding of a manually created subcomponent to its owner via `owner` and `source` options. // 3.7.0+ varPerson = san.defineComponent({ template: '<div>' + ' <input type="text" value="{=name=}">' + ' <input type="text" value="{=email=}">' + '</div>' });
Note: In cases where the manually created subcomponent with source option specified is expected to be created multiple times, the source template can be compiled manually to avoid San compiling it each time the subcomponent is created, as a measure of performance improvement.
In 3.7.1 and latter versions, the source template allows child elements to specify slot contents to be inserted into the manually created subcomponent.