SAPUI5 Call method's model from xml view - sapui5

Is it possible to call a method from model?
For example I have extanded my JSON Model and add a function called hello_world.
In my XML view I want to call the method of this model from and event off a control like below :
<Select change="{cl_vehicule>}.hello_world">
<items>
<core:Item text="1" key="1"/>
<core:Item text="2" key="2"/>
</items>
</Select>
Is it possible ?
If yes how to ?

It's not possible for the following reasons:
1) change in your Select control is an event, so it has to be associated with a function. This is not the same thing as calling the function. Please notice that you do not add () after the method name when using a event
2) cl_vehicule in your example should be the name of an association between a control and model. Models have no name. The name you put before > in a data binding is specified in the manifest or in a setModel method call. In other words, the same model can be associated with different controls but using different names.
3) Using {} means that you are using method bindProperty or bindAggregation to that control. These methods are defined in ManagedObject class. Those cannot be used in events.
4) As UI5 uses MVC paradigm, a View should never contact a model directly
So, you should basically use the change event associating it with a function from your controller. In that function you have different ways of getting your extended model to call a method.

Related

Number of converter instance for binding in UWP

How many instance does binding creates internally for converters.
<Image x:Uid="DisplayedImageUrl" Style="{StaticResource ImageStyle}"
Source="{Binding DisplayedImageURL, Converter={StaticResource ImageLogoConverter}}" />
How many instance does of ImageLogoConverter will be there?
Is it good idea to use converter in ViewModel, if not then what is the best way to access converted value of ViewModel property.
Is it good idea to use converter in ViewModel?
No. Why would you use a converter in a view model where you can return the converted value directly? Converters are used in the view, typically to convert a non-view friendly value that the view model returns.
If not then what is the best way to access converted value of ViewModel property?
You can simpy return an already converted value from the view model, i.e. instead of binding to a Uri property, you may bind directly to an ImageSource property.
This is the recommnded approach if you for example intend to display a lot of elements in a ItemsControl. Then you probably don't want to invoke a converter for each visible element for performance reasons.
I suppose you created the converter as a resource like this:
The number of instances now depends on the scope where the converter resource is declared. If you create it in <Page.Resources>, one instance will be created to be used by the page. If you create it in App.xaml in <Application.Resources> it will be an application-wide instance. Of course, you can even use a narrower scope - create it as a resource of a single control in your XAML tree for example - in any case, a single instance is created when instance of the parent is created.
The situation gets a bit more interesting if you embed it in a ItemTemplate of a list control. Thanks to virtualization, the system will not actually create one instance for each item. Instead, it will create only so many instances as fit on the screen and they get reused when the user scrolls.
Some MVVM developers don't like value converters, but others use them extensively. It really is a matter of preference. In cas you expect the underlying data to change often, it is advisable to keep the code in the converter as performant as possible as it runs on the UI thread.

Why no "insertAssociation" function?

I am writing a custom control and have an association declared like this:
details: {type: "sap.m.IconTabFilter", multiple: true, singularName: "detail"}
In the debugger I can see that there is an addAssociation function available, but there is no insertAssociation function (I am extending from sap.m.ResponsivePopover if that makes a difference).
My use-case is that I have an sap.m.IconTabBar that is internal to my control which I populate internally. But I also need to allow consumers to pass in their own custom tabs.
I want consumers to be able to instantiate my control using XML view types for example, so I am trying to expose a "details" association so they can seamlessly add the custom tab without having to create their own IconTabBar.
Is my understanding of associations incorrect?
Declaring it multiple says to the framework to store the association as an array. You are getting a method called getDetails() for this association. And also an addDetail and a removeDetail() method. Not sure if I understood your question because if you have a addDetail to add content to the association why do you want the insert?
J.

Trying to understand list selection in MvvM pattern with ICommands (no specific framework)

First note that I am not referring to any specific framework or technology like XAML.
The question is how to implement the MvvM pattern using ICommand for selection of an item in a list (=clicking a row)?
I have a view model (pseudo code):
class ListViewModel
{
// Items in the list.
public ObservableCollection<T> Items {};
// Command for item selection.
public ICommand ItemSelectedCommand
{
...
}
// Select an item in the list.
public void SelectItem(int index)
{
...
}
// The current selected item.
public T SelectedItem
{
get { ... };
}
}
How would I now connect my UI to that view model "manually"? Say, for instance in an iOS application.
I would probably have a UITableViewController, get an instance of the view model and populate the UITableView contents from it. Then I would trigger the ICommand from the RowSelected() method.
And here comes the thing I don't understand: how does the view model now know which item index was selected? I don't want to call SelectItem() because then I would not need the loosely coupled ICommand at all.
And maybe here we have to look how it is solved in XAML to understand the trick?
Coming from XAML and WPF, there are two options to forward selection changes from the UI to the ViewModel (as I understand your question, you're not asking about the other way around - feedbacking changes in the ViewModel to the UI - here):
Command with payload
The ICommands Execute method has a payload parameter. Executing a command without a payload can be done passing null:
SomeCommand.Execute(null);
In your case, it would make sense to pass the selected item as the parameter in the event handler:
vm.ItemSelectedCommand.Execute(eventArgs.SelectedItem);
or
vm.ItemSelectedCommand.Execute(myList.SelectedItem);
In the command's execution method, you can handle the parameter. Note that your ViewModel property SelectedItem is not directly involved here. If you need the selected index explicitly (which is not the case, usually), I would check the selected item's index in the Items collection.
Binding selected item of list to a ViewModel property
Option B is to 'bind' the selected item of the list to a distinct property on the ViewModel, in your case the SelectedItem property in the event handler of the list:
vm.SelectedItem = myList.SelectedItem;
The command is kind of redundant then, although you could invoke it without a payload after setting SelectedItem on the ViewModel. I would rather handle the change of the selected item in the set accessor of the property on the ViewModel.
Note: XAML and WPF come with quite a lot of infrastructure code out of the box. MVVM doesn't make sense without a proper framework to actually take care of binding UI and ViewModels in a loosely coupled way. You quickly end up with a lot of extra work and little benefit, because you're still maintaining tight dependencies. Bottom line: I recommend getting or writing a proper MVVM framework, before actually implementing it.

access class property in workflow foundation

so i am already invoking a workflow activity, with the "dataSet" containing an instance of my class Employee...
WorkflowInvoker.Invoke(wfManager.Activity, dataSet);
My Employee has a property called Department.
What i want is to edit a condition in my workflow xaml, but i am going to make it like this:
Employee.Department == "RND"
i am doing this in VS2012, and when i type Employee in the condition editor, i cannot access its properties. I just put my activity xaml file in the same project with my Employee class. What should i do more to access my class properties inside the activity xaml?
Basically you need to define an InArgument and map the external data to the arguments using a Dictionary. With that in place the object is available through the argument name.
See this blog post on how to pass data to an workflow.

How to create instance to zend controller

I have a controller named class TestController which extends some Zend_Controller_Action. Now I would like to use create an instance of TestController in TestForms (a Zend_Form). I want to populate a Zend_Form_Element_Select dynamically.
Please suggest how I can do this. Thanx in advance.
Where are you instantiating the form - is it in the controller? Instead of having the form call an action on the controller to dynamically get the values, you should look at setting the values on the form after it has been instantiated.
A quick and dirty way of doing that would be to grab the values in the controller and assign it to the element via:
$values = $db->query('query');
$element = $form->getElement('dynamicSelect');
$element->setValue($values);
Of course having DB queries to a table in your controller isn't exactly best practice... Per philistyne's suggestion, I use a a form builder class to build forms dynamically from my models. I have mappers for each model, and I pass in the mapper to the form builder class so it can dynamically populate my select elements.
A couple of things to try (passing a controller into a form or instantiating from within one is not recommended):
Use a model to access the dynamic values you want to put into your Zend_Form_Element_Select.
If the form is complex, create a form builder class to take care of, and separate out, the heavy lifting of the form construction.
Create customised form elements by extending from Zend_Form_Element_(Radio, Select, etc etc) if you feel you need very fine control over the form element's construction/behaviour/appearance, but wish to be able to reuse that element elsewhere.