Sapui5 use my new controle in xml - controls

this is how i create new control:
https://sapui5.hana.ondemand.com/sdk/#docs/guide/91f0a8dc6f4d1014b6dd926db0e91070.html
sap.ui.somelib.SomeControl.extend("my.OwnControl", {
...
init: function() {
if (sap.ui.somelib.SomeControl.prototype.init) { // check whether superclass implements the method
sap.ui.somelib.SomeControl.prototype.init.apply(this, arguments); // call the method with the original arguments
}
//... do any further initialization of your subclass...
}
Is possible to use some controle in my xml view ?

When defining a control like this:
sap.ui.core.Control.extend("my.Square", ...
and declaring the XML namespace
xmlns:my="my"
you can use your control like this:
<my:Square text="test" size="100px"/>
BUT only if you also declare your control as existing module:
jQuery.sap.declare("my.Square");
(this last part is missing in the other answer)
Here is a running example with the "Square" control from the documentation:
http://jsbin.com/zuxebev/1/edit?html,output

Related

Jasmine Unit testing define library that was imported as a global variable

I have a project that uses pdfMake to generate a PDF. To use it I include the file in my index.html
<script src='js/pdfmake.js'></script>
<script src='js/vfs_fonts.js'></script>
Inside pdfmake.js it declares global["pdfMake"] which then allows me to use the library in my service.
pdfService:
pdfMake.createPdf(docDefinition).download(fileName);
Everything works great but when I tried to test ths method in my service I get an error that the test can't find the variable pdfMake. That makes sense considering it's loaded by index.html.
How can I replace this library with a mock in my test?
I've tried using a spy but since makePdf isn't a function that doesn't work. spyOn(service, 'makePdf').
I tried just setting it as a variable but that also didn't work and I get: Strict mode forbids implicit creation of global property 'pdfMake'
pdfMake = {
createPdf: jasmine.createSpy('createPdf').and.returnValue({
download: jasmine.createSpy('download')
}
}
I got the same problem and solved inserting the pdfMake mock on global variable window inside the unit test. So, in your case will be something like this:
window.pdfMake = {
createPdf: jasmine.createSpy('createPdf')
.and.returnValue({
download: jasmine.createSpy('download')
}),
};
I just fixed this issue by making below changes-
Declare pdfMake variable globally in your .ts file like-
declare var pdfMake;
And then mock the pdfMake function in your .spec file like this-
window['pdfMake'] = {
createPdf: function (param) {
return {
open: function () {
return true;
},
download: function () {
return true;
}
};
}
};

Gradle extension as NamedDomainObjectContainer of Closure

I'm trying to build a Gradle plugin that would allow the following:
myPluginConfig {
something1 {
// this is a closure
}
somethingElse {
// this is another closure
}
// more closures here
}
To achieve this I'm fairly certain I need to use a NamedDomainObjectContainer to wrap a Closure collection, so I've set up the following plugin:
class SwitchDependenciesPlugin implements Plugin<Project> {
void apply(Project project) {
// add the extension
project.getExtensions().myPluginConfig = project.container(Closure)
// read the current configuration
NamedDomainObjectContainer<Closure> config = project.myPluginConfig
// test it out, always returns []
System.out.println(config)
}
}
What am I doing wrong, do I need to use project.extensions.create instead? If so, how?
EDIT: my use case consists in adding dependencies according to some variables defined in the project hierarchy. For example, the following configuration would add the red project if the variable red is defined on project.ext, or gson otherwise:
myPluginConfig {
redTrue {
compile project(':red')
}
redFalse {
compile 'com.google.code.gson:gson:2.4'
}
greenTrue {
compile project(':green')
}
}
For this use case I need to have dynamic names for myPluginConfig, and therefore either a Map or a NamedDomainObjectContainer.
Can you elaborate what you try to model here? I think you have two options. One is to use NamedDomainObjectContainer. Here you need a class that represents "something". Have a look at the Gradle userguide chapter about maintaining multiple domain objects (see https://docs.gradle.org/current/userguide/custom_plugins.html#N175CF) in the sample of the userguide, the "thing" is 'Book'. The build-in configuration syntax like you described above comes for free.
If you want to have a syntax like above without the need for maintaining multiple domain objects, you can simply add a method that takes a Closure as a parameter to your Extension class:
void somethingToConfigure(Closure) {
}
You cannot have Closure as a type for NamedDomainObjectContainer simply because the type you use must have a property called name and a public constructor with a single String parameter.
To overcome this, you may create a wrapper around Closure with a name field added.

Typescript and Extending kendo DataSource

I'm using KendoUI and typescript in my application and i want convert this code to typescript:
var customDataSource= kendo.data.DataSource.extend({
init: function (options) {
kendo.data.DataSource.fn.init.call(this, options);
}
});
But typescript can't convert and display error in this line:
kendo.data.DataSource.fn.init.call(this, options);
what is wrong?(I'm using kendo.all.d.ts)
A simpler quick fix:
Just use any:
(<any>kendo.data.DataSource).fn.init.call(this, options);
Complex better fix :
TypeScript cannot infer that init is now a member function of kendo.data.DataSource. Depending upon what the type of kendo.data.DataSource is you can tell typescript about it. I am assuming the type is something like kendo.data.IDataSource
declare module kendo{
module data{
interface IDataSource{
init:Function;
}
}
}

How to use an external jquery plugin with Script#?

How can I use an external jquery plugin with Script# 0.7? Is there a tool to convert any jquery plugin to equivalent c# code? Or we have to do it manually?
Depending on your exact code, the following might also be appropriate:
jQuery.Select("#myDiv").Plugin<jQueryWithFoo>().FooInit();
This is useful if you have multiple plugins you want to use, and use the fluent-API pattern that you would in regular jQuery. Example:
jQuery.Select("#myDiv").
Plugin<jQueryFoo>().FooInit().
Plugin<jQueryBar>().SomeBarMethod();
In my opinion it's usually a better use of one's time to include the plugin as-is (in it's JavaScript form), and then prepare an imported type in Script# for exposing the plugin's functionality to the rest of Script#.
I don't know if there is a shortcut approach in Script# when dealing specifically with jQuery plugins, but what I've quickly done in the past is something like the following:
// Import my plugin "Foo"
[Imported]
[IgnoreNamespace]
public class jQueryWithFoo : jQueryObject
{
private jQueryWithFoo () { }
[ScriptName("foo")]
public void FooInit() { }
[ScriptName("foo")]
public void FooMethod(string method) { }
[ScriptName("foo")]
public void FooMethodWithOptions(string method, Dictionary options) { }
}
Then to use the plugin on an object you just cast to your imported type:
// grab my div and cast to my plugin type
jQueryWithFoo myDiv = (jQueryWithFoo)jQuery.Select("#myDiv");
// use the plugin
myDiv.FooInit();
myDiv.FooMethod("toggle");

Lazy generic delegate initialisation using Ninject

I'm using Ninject 1.0 and would like to be able to inject lazy initialisation delegates into constructors. So, given the generic delegate definition:
public delegate T LazyGet<T>();
I'd simply like to bind this to IKernel.Get() so that I can pass a lazy getter into constructors, e.g.
public class Foo
{
readonly LazyGet<Bar> getBar;
public Foo( LazyGet<Bar> getBar )
{
this.getBar = getBar;
}
}
However, I can't simply call Bind<LazyGet<T>>() because it's an open generic type. I need this to be an open generic so that I don't have to Bind all the different lazy gets to explicit types. In the above example, it should be possible to create a generic delegate dynamically that invokes IKernel.Get<T>().
How can this be achieved with Ninject 1.0?
Don't exactly understand the question, but could you use reflection? Something like:
// the type of T you want to use
Type bindType;
// the kernel you want to use
IKernel k;
// note - not compile tested
MethodInfo openGet = typeof(IKernel).GetMethod("Get`1");
MethodInfo constGet = openGet.MakeGenericMethod(bindType);
Type delegateType = typeof(LazyGet<>).MakeGenericType(bindType);
Delegate lazyGet = Delegate.CreateDelegate(delegateType, k, constGet);
Would using lazyGet allow you to do what you want? Note that you may have to call the Foo class by reflection as well, if bindType isn't known in the compile context.
I am fairly certain that the only way to do this (without some dirty reflection code) is to bind your delegate with type params. This will mean it needs to be done for each individual type you use. You could possibly use a BindingGenerator to do this in bulk, but it could get a bit ugly.
If there is a better solution (a clean one) I would love to hear it as I run into this problem from time to time.
From another similar question I answered:
public class Module : NinjectModule
{
public override void Load()
{
Bind(typeof(Lazy<>)).ToMethod(ctx =>
GetType()
.GetMethod("GetLazyProvider", BindingFlags.Instance | BindingFlags.NonPublic)
.MakeGenericMethod(ctx.GenericArguments[0])
.Invoke(this, new object[] { ctx.Kernel }));
}
protected Lazy<T> GetLazyProvider<T>(IKernel kernel)
{
return new Lazy<T>(() => kernel.Get<T>());
}
}

Resources