sapui5 binding callbacks, update vs. insert vs. add - sapui5

Looking through sap.ui.base.ManagedObject has helped make the data binding in sapui5 more clear, but it's still not quite tangible yet.
A fundamental question that isn't clear to me is which callbacks get fired on a control when it's model is updated.
For example, if a control had an aggregation called items, in which situations does "updateItems" get fired? And which situations does "addItem" get fired? Also, "insertItem"?
I see that addAggregation and insertAggregation have a parameter to suppress invalidation. But I don't see that option for updateAggregation. Is there a reason for that?
Does the "bindable" metadata property alter this behavior at all?
Thanks for any insight!

Related

useQuery hook vs queryClient.fetchQuery, when to prefer one over the other

queryClient.fetchQuery can be used in place of useQuery hook provided by react-query. Any guidelines, best practices on in which scenario, one should be preferred over other.
One scenario might be where we want to conditionally fetch data. So, queryClient.fetchQuery looks more natural while useQuery hook not.
Is the above logic correct? Please advise on the best practise of using one over the other
fetchQuery is an imperative way to fetch data. You cannot call this during rendering, because it would be a side-effect and you cannot await the result. So you would need to spawn a useEffect.
The other difference is that useQuery creates a subscription to the queryKey, but fetchQuery does not. So if the data updates for some other reason (like a second component mounts and triggers a re-refetch of the query, or you refetch in the background because you focus the window and the query is stale), your component will not re-render and show the new data.
One scenario might be where we want to conditionally fetch data.
This is where you want the enabled option of useQuery, so that you can disable your query for as long as the conditions are not met.
The bottom line is: The two are not interchangeable, I have almost never used fetchQuery. For components, always useQuery

Updating Metadata Annotations

I am using kubebuilder to create a Kubernetes operator. When an object of my kind is initiated I have to parse the spec and update the objects based on a few calculations.
From what I can tell I can either update the status of the object, the metadata, or a managed field (I may be wrong?). It appears that the sigs.k8s.io/controller-runtime/pkg/client library is responsible for how to update these fields (I'm not completely sure). I am having trouble understanding the docs.
I have the following questions:
Are there a guide to best practices about where to store configuration on the object between status, metadata (labels or annotations), and managed fields?
How do I update/patch the annotations of an object similar to how I would use r.Status().Update(ctx, &thing); to update the status?
The Kubebuilder docs are a bit raw but nonetheless are a handy guide when building CRDs and controllers with Kubebuilder. It walks you through a fairly detailed example which is great to study and refer back to, to see how to do certain things.
The answer to your question generally is, "it depends." What values are you calculating, and why? Why do you need to store them on the object? Is the lifecycle of this data coupled to the lifecycle of this object, or might this computed data need to live on and be used by other controllers even when the object is deleted? In general, is anything going to interact with those values? What is it going to do with them?
If nothing else aside from the reconciliation controller for the CRD is going to interact with the data you're putting, consider putting it within the object's Status.
Doing r.Status().Update(ctx, &thing) will avoid triggering any side-effects as it will only persist changes you've made to the object's Status subresource, rather than its spec or metadata.
A common thing to do with custom resources is to set and remove finalizers, which live in the object's metadata.

Scala ScalaFX: how to deal with large set of changes of Observable*

I am using a ObservableMap for data modeling, and want to update the whole entry. Initially the ObservableMap is empty, and it is filled asynchronously with lots of elements.
Now the problem is that an onChanged event is shot for each and every entry, which creates too many events and a bogging down of the GUI. I used pkgs.clear() ; pkgs ++= newpkgs.
Is there way to either only trigger one onChanged, either by disabling the handler temporarily, or by having an operation on the map that updates all elements but fires only afterwards.
I'm not aware of a mechanism to disable/delay/buffer UI updates.
I don't know about other JavaFX view types, but I have experience with this using TableView in a similar situation. I suspect other views may be similar in this regard.
At least the TableView has a property called itemsProperty. When data in itemsProperty is updated using setItems (wrapper for itemsProperty.set(value)), the table tries very hard to update just the minimum slice of it self.
However, for the optimizations to work, the key is that the items must be "value objects" (hashCode and equals are deep and based on the actual data being displayed and not some random references.)
In the case of TableView this may require elaborate rowFactory and
cellFactory implementations. The reason is that the data in items can't be "preformatted" in any way or it would spoil the optimizations inside TableView.
Realizing the above, solved my update churn problems. Maybe other answers can provide other tips.

Mongoid: correctly using association callbacks with forms

I have come across a problem which I am struggling to solve elegantly. I am more versed in RDBMSs so the way I am doing things may not be ideal.
What I am doing:
I am having to keep track of items within a HABTM association. Whats more, there is a condition on the count as only items that are 'active' are counted. I have successfully used the association callbacks to track additions and removals from the collection.
The problem:
I am also adding items to the collection via forms by setting the opposite instance's id to the form as a hidden field. This works fine, however the problem is that this adds the opposite instance straight to the collection without invoking the callback (the age old problem).
My Question:
Is there a more elegant way to add add instances to the collection that invokes the callback?
Let me know if you need any more specific examples and I'll happily provide some.
Have you try using the following mongoid3 callbacks?
after_add
after_remove
before_add
before_remove
More information here

Multiple Models

I like knockoutjs, the sooner we get rid of coding directly toward the DOM the better. I'm having trouble understanding how I would do something which I'm going to explain in terms of a question/answer site. (This is probably a general MVC/MVVM question)
In my data model I have a question[id, description] and answer[id, question_id, text]. The browser requests a list of questions which is bound to a tbody, one column will display the question description, while the other should be bound to an answer textbox.
One obvious way of doing this is to have a QuestionAnswer[question_id, answer_id, question_descrition, answer_text] model. Ideally I'd like to keep them separate to minimize transformation when sending/receiving/storing, if there isn't some way of keeping them separate then I have the following question:
Where is the ideal place to create the QuestionAnswer model ? My bet is that by convention its created on the server.
If there is such an example somewhere please point me to it, otherwise I think it would make a good example.
Please help me wrap my head around this, Thanks!
What you could do is to create the combined model on the server, serialize it to json and then use the mapping plugin to add the serialized list to the view model.
I'm doing that here only it isn't a combined model, but shouldn't make any difference. Especially since it seems like your relation is one-to-one.
If you need to create an "object" in your view model, you can use the mapping definition to do so, like I do here.
I use C# to build my model on the server, but I guess you can use whatever you are comfortable with.
The cool thing with the mapping plugin is that it adds the data to the view model so that you can focus on behaviour.
Ok,
I'v gathered my thoughts on what my question is actually asking.
To do data binding on the client side you obviously need your data model there as well. I was conflicted on what I needed to send over and at what time.
To continue with the Question/Answer site idea: Sending down a list of answers each of which have a question in them is what should be done. That way you can bind to the answer list and simply bind the question description of each answer to the first table column.
If later I want to make a question editor I would potentially send a complete different data structure down and not reuse the Answer has a Question structure previously used.
I thought there might be a way of sending down a more complex data structure that references itself. Which apparently is possible in JSon with some extra libraries.