How to access SAPUI5 component from utility functions? - sapui5

I would like to access my SAPUI5 app component using this.getOwnerComponent() from a utility function, for example formatter or a class function, this works from controllers but in a utility or formatter function placed in another folder doesn't work. I do not want to use sap.ui.getCore(), is there any other way to do it?

I ended up with below two choices:
1. Create the utility functiona as a singleton class and pass the component one time with a setComponent method.
2. Use event bus to fire events and get things done by component that can be done only within the component like accessing oData resources

You could pass the controller instance to your utility class in the constructor, and then access its owner component. You could also try injecting the component directly. Unfortunately, I am not sure if any of these is the preferred way in UI5 development.
That would couple the utility function to the component logic though, which I do not think is a good idea, you probably should pass only what is required for the utility function to do its' job.

Related

How to distinguish wizards in Eclipse RCP?

We have an Eclipse IDE application on 3.x that uses various newWizards to allow the user to create different files. Although these files differ slightly contentwise, the structure of the wizards is quite similar.
Thus, a sound object-oriented approach would be to instantiate different wizards from the same class and initialize them with different data.
Problem:
To decide what wizard needs which data we need a way to distinguish the different already instantiated wizards (e.g during the call to the init method of the wizard).
Is there any way to do so? It would e.g. help if somebody knows a way to get the wizard's id defined in the extension point from within the instantiated wizard.
If your wizard implements IExecutableExtension, it will be passed the configuration element that represents the extension for which it is created.
You can also use extension factories in that you specify a type that implements IExecutableExtensionFactory.
The interface allows you to control how the instances provided to extension-points (wizards in your case) are created.
Extension example:
<extension point="org.eclipse.ui.wizards">
<newWizard
name="..."
class="com.example.WizardFactory">
</newWizard>
Note that the extension factory may also implement IExecutableExtension to gain access to extension attributes before creating the extension's executable class.

Is there a shortcut to copy a method to the interface?

I rather like the "Extract Interface..." refactoring function when working on a class, but it only allows you to extract to a new interface. I was wondering if there was a similar option that allowed the user to copy a method(s) to an interface(s) that the current class already implements.
It's something that would save me a lot of time but despite thinking I may have done something like that before, I can't find any reference to such a function. Does anyone know if it even exists?
This refactoring is called "pull up method." You can move a method to a superclass, or add its declaration to any of the interfaces a class implements.

Using only part of a class from a different target

I just created a new target for the Lite version of my app. The Lite app only uses part of a base class that I have in the main app, ie it won't need to use an option that requires it to import 4 or 5 files.
My question is, from a design perspective, what is the best way to handle this so that my Lite version can only use the part of the class that it needs? Obviously, one solution is I just import those 4 unnecessary files into Lite build phase, and just use the whole class (even the parts it doesn't need). This seems inefficient though. I know I can do an ifndef to block those files from being imported if the Lite version is running, but how do I block out the code in the class from also not being picked up by the compiler?
Would a better way just be to have my Lite version create a subclass of the Base class that only uses the options it needs? But then I believe, would I still need to import those unnecessary files?
Just a bit confused about this, first time I've ever created another target that utilizes code from the main target. Any help appreciate thanks.
Put the common/lite functionality in a super class. Heavy functionality in the sub-class.
As another answer points out, you can handle this by putting the lite functionality in a subclass and the full functionality in a superclass.
Another option is to use a single class, and add the full functionality in an Objective-C category. Essentially, you can define methods in the category to supplement – or replace – methods in the base implementation.
Unlike a subclass, however, methods defined in a category can't invoke super to get the base class's functionality. super still refers to the base class's superclass, whether that's NSObject, UIDocument, or what have you – not the implementation without the category.
The advantage is that you only have one class name, so the code which instantiates your class (or classes) doesn't need to use something like #ifdef to switch classes and #includes depending on whether you're building the lite or full version.

IoC, MVC4 Web API & HttpParameterBinding/ParameterBindingAttribute

I'm using ASP.Net MVC 4 RTM Web API. I have a controller action with a parameter that I'd like to populate via custom model binding. To achieve this, I created a class that derives from System.Web.Http.Controllers.HttpParameterBinding that sets the value of this parameter. I then created an attribute class that derives from System.Web.Http.ParameterBindingAttribute which I use to decorate the parameter on my controller action.
This is all working great, my HttpParameterBinding class is populating the action parameter correctly. The problem I have is that my custom parameter binding class has a dependency that I'd like resolved via my IoC container (Unity). Is there a way to override how Web API creates HttpParameterBinding instances so that I can build up my custom binding class dependency from Unity? I was able to do something similar for a filter attribute by creating a custom filter provider that uses Unity's BuildUp method to populate dependencies, however I'm not seeing anything similar for Web API's HttpParameterBindings.
In general: to use IoC / Unity in the Web API you need to set it up seperately.
Try downloading the nuget package Unity.WebApi and see if that helps!
Take a look at this article: Parameter Binding in WebAPI
It walks through a couple different options from Converters to Binders to BinderProviders. It sounds like you may be able to write a custom ModelBinderProvider which knows how to provide your dependency. If that isn't high enough in the chain you can look at replacing the default IActionValueBinder service. It's a DefaultActionValueBinder instance, which you can extend or simply re-implement.
I also highly recommend downloading the WebAPI source code, as it's been an incredible help for these issues as I've run into them. Here's the WebAPI source code. I recommend downloading it so you can open it in VS for easy navigation.
Feel free to check out FlitBit too (It's very modular, don't let the number of packages scare you off)! I'm working on a WebAPI package for supporting FlitBit, specifically FlitBit.IoC and FlitBit.Dto. I'll add an update if I work out my IoC issue, since it's very similar to yours.

Migrate custom Facebook util library to Yii framework

I have a facebook app developed in plain PHP, I'm migrating the app to YII framework.
The thing is that I use a class call "utilsFacebook" where I have the object facebook(of the fb sdk) and all the methods that I need to get data from facebook, getUserId, getUserFriendList, etc.
I don't know how to handle all the operations that I do in utilsFacebook with Yii.
Create a controller with the functions of utilsFacebook is the correct think to do?
Every time that I instance the controller would create a new Facebook object, Should I store that object in a SESSION to get a better performance or is a bad idea?
Q. Create a controller with the functions of utilsFacebook is the correct think to do?
Having done a facebook app using yii as the framework, i would recommend you to make this library either a component, or an extension.
But definitely don't put these functions in the controller directly. Whenever a controller needs them call the functions using your custom facebook util class.
Components can be put in the folder: projectrootfolder/protected/components
Extensions can be put in the folder: projectrootfolder/protected/extensions
If you don't believe that either of these make semantic sense, you can always create a new folder within protected, say utils and put the class there. However i think extensions is the best way to go.
Q. Should I store that object in a SESSION to get a better performance or is a bad idea?
I don't think it's necessary to store the object in a session, because there will be no visible performance gain. Further you'll complicate your code unnecessarily.
What i had done was, created an app level component and used this component throughout the app, in any controller.
Example:
In your application's config, protected/config/main.php :
'components'=>array(
'fbHelper'=>array( // gave the component this name
'class'=>'ext.utils.FacebookHelper', // had stored the helper class in extensions/utils folder
'parameter1'='somevalue',
// more parameters
),
// standard yii app components
),
This will allow you to use the component like this: Yii::app()->fbHelper->getFriends();
Take a look at the facebook-opengraph extension, which could help you, on the way.