Friday, 24 May 2013

28.CUSTOM CONTROLLER AND DATA BINDING



Views lost its data as soon as user navigates away from the view. In order to solve this problem, Custom controllers are introduced. 

We add same type of context nodes to the custom controllers and do the data binding between the custom controller context node and required view context node. 

One UI component can have any number of custom controllers.


In our case, we will follow below logic for making data flow across context nodes of different views.

1.       We will fetch data in the search event handler ( that we completed ).
2.       We will fetch instance of custom controller and feed the data we fetched to its context node. 

3.       We will do the binding between result view context node and custom controller context node so that data will travel to the result view.

Let us create custom controller first. 

Go to the UI component – component structure browser. Right click on CUSTOM CONTROLLER and choose option to CREATE. 

Give the name that has prefix CuCo*.ex: CuCoSearch  CuCo stands for custom controller.
In the next step, give context node name as result and entity name as BTQRSrvCon, because we will bind this node to the result view context node. 

Context node base entities should be same for binding.
Just complete the wizard by choosing continuebutton  and save the entries.  We have successfully created custom controller.






Next we will bind the two context nodes. Go to the result view and  Right click on the result context node  and choose the option create binding.









In the next pop up , choose the controller type as custom controller as we are binding the data to result view.
Choose CuCoSearch.do as view controller in the custom controller field and choose the result context node and press on right mark.  That’s it, we have done the binding. 







once above step is done, We can observer the binding here.






It is saying the result context node of custom controller is bound to the result context node view RESULT.
Next we need to add small part of code to the search event handler to get the custom controller instance and feed the data that we have already fetched. 

Go to the search event handler of search view and add the below code to it. 







At line no 16, we used the method get_custom_controller to fetch the instance of custom controller by giving its name. 

We are giving the returned data lr_result to the context node result of custom controller. 


Test the application now just click on the search button. It will show some service contracts. Our search page is working now. 




9 comments:

  1. Great.

    I want to clarify one moment, in this article you define custom controller in order to store the data, but there is another way to store the data — context node of component controller.

    My question is what is the desirable way to implement such task? What are the pro & contra of each of the approaches? Differences in performance, memory etc?

    Thanks.

    ReplyDelete
  2. This tutorial is awesome, by far the best introduction to SAP Web Client programming one the Web. It's been an invaluable help to me. I really like your approach: Rather than showing the quickest way to build an application, you show the quickest way to learn how to build an application. Kudos! I really hope you continue to evolve this site.

    ReplyDelete
  3. For the question posted on 8 July 2013,

    For sending data across views in a single component, we need to use the custom controller. If the data is to be sent from one component to another component, a component controller context node is used.

    You will need to fetch the instance of the Custom Controller/Component Controller if we need to get data. Theoretically a component controller should occupy more memory.

    ReplyDelete
  4. Hello!

    I suppose you have a mistake in the begining of this article.

    instead of "one ui components can have any number of COMPONENTS" mus't be "any number of custom controllers".

    P.S. your blog is greate

    ReplyDelete
  5. Thanks for identifying the mistake. I corrected it.

    ReplyDelete
  6. What is bif. B/w right click and create binding and what about the method Create_context_binding, when this are implemented.. Kindly Let Me Know...

    ReplyDelete
  7. very helpful blog. Thank you

    ReplyDelete
  8. An easy approach to explain the complex details while covering the necessary aspects of theoretical contents as well. Your blog articles are quite good for any beginner like me.

    Thanks...

    ReplyDelete
  9. thank you very much

    ReplyDelete