Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 3019

Re: Reusabilty in SAPUI5

$
0
0

Hi Jun,

 

I am still not very clear on the concept of re-usability. As I am novice in UI5 domain, it gives me tough time to understand the details which you have mentioned.

 

Just to state my aim very clear, I am trying to develop a view and it's event handlers reusable. For example:

 

MyFrag.fragment.xml

<core:FragmentDefinition xmlns="sap.m" xmlns:core="sap.ui.core">

<Input id = "MyInput" liveChange="onInputChanged")

</core:FragmentDefinition>

 

To test this fragment, I have created a view (Say App.view.xml) which embeds the fragment and the controller (App.controller.js) which defines onInputChange event handler.

 

App.view.xml

<mvc:View controllerName="com.test.controller.App" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m" xmlns:core="sap.ui.core">

  <App>

  <pages>

  <Page title="{i18n>title}">

  <content>

     <core:Fragment fragmentName="com.test.view.MyFrag" type="XML" />

  </content>

  </Page>

  </pages>

  </App>

</mvc:View>

 

App.controller.js

sap.ui.define([

  "sap/ui/core/mvc/Controller",

  "sap/m/MessageToast"

], function(Controller, MessageToast) {

  "use strict";

 

 

  return Controller.extend("com.test.controller.App", {

    onInit : function(){ }

  });

  onInputChanged : function(oEvent){

     MessageToast m = new MessageToast();

     m.show("Test");

  }

});

 

 

Now, I have another project with View MyView.view.xml (full name of the view is com.real.view.MyView. My reusable fragment't event handler resource root is com.test.view.*) where I want to reuse MyFrag.fragment.xml and onInputChanged event handler (similar to App.view.xml and App.controller.js mentioned above. However, instead of copy pasting the event handler code to MyView.controller.js, I want to put it in .js file and reuse it).

 

So my question would be: How can I make the event handler generic and reusable? Can you show it with an end to end example (as I am not an expert, I am not understanding where to write what, so asking for the complete code)? If you can share the project source code completely, that would clear all my questions.

 

Also, is there any way to create a library of MyFrag.fragment.xml and it's event handlers and provide it to developer so that they can make use of it like other javascript files (instead of copying view fragment and corresponding javascript files explicitly)? If yes, how can I do that?

 

Thanks,

Faddy


Viewing all articles
Browse latest Browse all 3019

Trending Articles