-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom Control + Model/Aggregation handling best practices(?) #13
Comments
After some more research on how to properly work with aggregation I've finally wrapped my head around it (I think/hope :^)). There are two different PoVs to consider. Will it be used from the "outside" so with Binding (e.g. from the XML View or ofc via a controller etc.) or from the inside, within the control. What finally made it click better for me was the comment here. You won't find any manually created "rows"-aggregation within the control, so no magic These will be created from the binding mechanism itself behind the scenes for you. Like the Based on more UI5 Standard that I've crawled through it is also perfectly fine to create UI5 controls within a control. I was wondering whether or not this is ... "dirty" because it kind of makes you feel that way as you can define a custom control a renderer and all those things while the renderer itself can use basic HTML but you can still throw UI5 itself in there ... it can get confusing and I for one ended up questioning my decisions more than once and didn't know if things were "best practice" or "supposed to be like this" as it ... didn't feel "clean" but what is clean anyway? It just really depends how you look at it and as I can tell by now, it is perfectly fine. An example would be the MessagePage or even the custom control tutorial that even makes use of "private" aggregations so the PoV within this tutorial is full-on custom control development and nothing from the outside or rather to abstract the aggregations and their behaviour through a common interface (the value property) but manage them completely internally (within the control) without exposing them. A more specific example also based on the MessagePage would be:
Regarding the initial issueI've pivoted from handing over an entire object (with type object, so in the JS world ...) into a property to actually bind the data against some property like "boxes" or "segments". The data is the same, an array of objects (items). So from the outside there is the usage of the custom control where a JSONModel (ViewModel in this case) is bound against. At runtime the custom control of the outer box has an aggregation (of a custom item so to say) and due to the binding will have X custom items to render. I'll update this Issue once the "final" or rather improved approach is added to this repository as another custom control example. |
Basic question: What are the best practices when it comes to model handling with/within custom controls?
What is the use-case/problem?
Basically I'm retrieving data from the backend, this can have any shape i.e.
So retrieve Data via OData V2, set our data into our internal JSONModel (state etc.) and use it.
Now at best I'd want to bind/set this data to my custom control which, based on this, renders different aggregations. For example if there are 2 items, there are 2 aggregations, if it's 3 then 3 accordingly. These aggregations should also be able to use the values within the items for displaying certain information.
In a best case scenario I'd like to rely on the model itself and don't do much manual work. So whenever the model updates, I'd expect a "rerender" of my custom control with updated content/aggregations.
Now I have dug quite deep into UI5 itself and figured out that the "issue" is, that using
setProperty
for updating (if it isn't the root), updates the .. well .. property only and therefore all the references of it which leads UI5 to think that there are no changes that require any rerendering during diffing withinManagedObject.js
, or a bit deeper/earlier already withinModel.js
. This is because, as it is directly updated by reference the oldValue is equal to the new Value.Generally saying (even though probably not as performant) this can be ... worked around by using
setData
(whichsetProperty
uses under the hood for root changes) optionally also using themerge
flag ofsetData
. This however, technically, always creates a new object, thus creating a new reference which then again "fixes" my issue but doesn't seem "proper"/performant.This behaviour can be tested using this repository (the one the issue was created on)
UI5-control-example/webapp/controller/Main.controller.js
Lines 31 to 75 in d326578
setProperty
. You can observe that the model itself is changed but the UI won't get rerendered. While usingsetData
it does.All of this is easier with primitives which are more common use cases and examples shown everywhere. What came to mind was the
sap.m.Tree
(or maybe even the List itself as Tree is based on parts of it) as the Tree can take in quite a complex structure (as JSON Binding) and I think updating models, automatically takes care of updating everything there too. But ... I'd need quite a lot more time figuring all this out.The text was updated successfully, but these errors were encountered: