TypeScript extend object in module - object

What I want to do is really similar to this and this except I'm trying to figure out how to put an ArrayExtension inside a module.
I'm trying to get something similar to the way C# extension methods work, that way I can just import the module and I'll have my extra methods. The links I provided show how to extend an existing object, but I haven't been able to figure out how to encapsulate that into a module.

If you're targeting non-browser environments like node.js this will be possible because you will be able to pass references to your module's global members, such as Array, to other modules. Those other modules can then extend the passed in object and/or its prototype with extra functionality which will be only accessible by the calling module. Other modules would have to do the same in order to get these extensions; therefore, conflicts are minimized since imports are explicit.
However, in browser environments this is not the case since there is only one window object and any changes to its members are available everywhere. As soon as any of your modules extended Array those extensions would be available to all other modules -- increasing the possibility for conflicts and making the code harder to reason about.
With that said, there are patterns in JS, and therefore TypeScript, which should accomplish what you want. One such pattern is the 'mixin' pattern which allows you to add on extra functionality on an object instance basis. You could separate re-usable code into mixin modules which could then be applied to an object when needed, or even automatically in constructors. Take a look at this for a decent overview and implementation examples: http://javascriptweblog.wordpress.com/2011/05/31/a-fresh-look-at-javascript-mixins/

If you're trying to extend the built in Array type you can't do that within a module. You're extension will need to live in an ArrayEx.ts file and occur outside of any modules. The reason for that is that if you did it within a module you'd be extending the Foo.Array type which isn't the same as Array.
But you said you just want to be able import the module to have your extra methods show up and all you really need to do is add a /// <reference path='ArrayEx.ts' /> to any file you want the extension methods to be available to. This is essentially the same thing.

Related

Reusing a component in lit-element

Anyone that can point to any documentation on howto reuse code in lit-element.
The problem now is that if I declare an element, in my case a close-button and I want to reuse it by importing it into 2 or more lit-elements, there will be an error in the browser about the close-button being declared more than once.
Understandable enough, but how do I reuse a component, I could of course move the button to a separate file and add it to the document, but then there would be dependencies on that for other components to work.
Any suggestions
If close-button self-registers itself, with a call to customElements.define('close-button', ...), then you should be able to import its defining module and not have any errors due to the module caching behavior of JS.
You must have multiple customElements.define('close-button', ...) calls, so I'd make sure that 1) it's self-registering and you're not registering it again in each component that uses it, and 2) you're using standard JS modules.
After investigating a bit more, I concluded that sharing HTML templates might be the way to do it.

What is the correct way to import an ADT

I have been following the steps outlined here: https://dev.liferay.com/develop/tutorials/-/knowledge_base/6-2/importing-resources-with-your-themes
My custom structures and templates are being imported. The problem that I have is that I also want to define an ADT to be imported. Nothing happens when I create the directory structure outline here:https://dev.liferay.com/develop/tutorials/-/knowledge_base/6-2/creating-plugins-to-share-structures-templates-and-more
Is the "templates-importer" standard deprecated? Can the "resources-importer" and "templates-importer" not be used at the same time?
I don't see any exceptions in the log and there's nothing to indicate why the ADT is not being imported. I have checked in the site template that is generated and in the global site.
Suggestions? Do I really need to create a hook to import the ADT?
You can use only one of those two, but they share common code and functionality (here are if...else statements responsible for chosing one of). If you use Resources Importer already, you can add your ADT's to resources-importer folder (in the same way it is done within templates-importer) and it will be imported as well.
I have a hook project which resources-importer which contains (among others) 4 ADTs. They are located in the following folders:
resources-importer/templates/application_display/asset_category
resources-importer/templates/application_display/asset_entry
First one is imported as ADT for Categories Navigation portlet and second for Asset Publisher portlet.

How to create custom extension point in ReSharper plugin

We are working on plugin for ReSharper and we want to make our plugin extensible. Seems, we should use ShellComponent attribute to do it but we can not find any examples. Could anybody enplane how to define custom extension point and how to manage extension. Example of code of extension point and extension implementation would be very helpful.
Thanks.
If you're looking to write a plugin that can extend ReSharper, you need to tell ReSharper about the classes in your plugin, by marking them with the [ShellComponent] or [SoutionComponent] attributes. These attributes have different lifetimes - a shell component lasts the lifetime of ReSharper itself, and a solution component is created when a solution is opened and disposed when the solution is closed.
To make ReSharper do something useful with your components, they typically have to implement an interface, such as ICodeCompletionItemsProvider, and sometimes have to use a different attribute, such as [CodeCleanupModule] (which itself derives from ShellComponentAttribute). There are many extension points in ReSharper, and the one that's appropriate for you depends on what you're trying to do - refactoring, unit test provider, code cleanup, code completion items, etc. The devguide provides a good introduction to the more common extension points.
But if you want to make your own plugin extensible, then your component needs to work with a kind of provider pattern, by deferring work to multiple provider instances. For example, code cleanup works by deferring to multiple code cleanup modules, each responsible for cleaning up a different aspect of your code (whitespace, ordering, etc). To do this, your component should take in a collection of providers in the constructor. ReSharper's component model will automatically create a collection of these types and pass them to. More specifically, you should have a constructor that takes an IEnumerable<T> or IViewable<T>, where T is the interface of the provider you're going to define and call. The IEnumerable<T> will give you a simple collection of providers, but IViewable<T> represents an observable collection, and allows you to subscribe to notifications of new providers being made available from the component model.

require.js require inside a require

Why do we want to have another require structure inside a require structure?
like
require([mod1,mod2], function(m1, m2){
require([mod3], function(m3){
// and then will use m1 and m2 here as well
})
})
Why can't we just have one require structure? I want to understand the motivation between this setup.
Nested require isn't mandatory, and can easily be avoided if this don't fit your style.
Although, this can be useful to load submodules or conditional modules (like a polyfill).
In a more personal experience, I often use nested require inside my router controller in order to load certain page view when they're requested. This allow me to request only the dependencies of my router without loading the entire page collection of an app.
I also often find myself using nested require to manage some i18n aspect of some apps by loading conditional locale.
Last thing, I'd just remember that modules should be defined using define, not require. require function is really used to arbitrary load scripts if needed (and can be use once to bootstrap your app). So in most of the real use case, you'll have some nested require inside a define module definition.
Hope this help!

How to add "Using " statement at runtime using IWizard

at compile time we have
using MyNamespace;
This works till now but recently the requirement got change and it needs to handle at run time based on the application type selected by the user.
So, How can I add the "Using" namespace statement using c# code in the IWizard?
I know how to add the reference at run time ass under
var appProject = project.Object as VSProject;
appProject.References.Add(Mydll);
What I want is that at runtime
using System.IO;
using MyNamespace-> should come at runtime based on the application selected
Thanks
I just hit a similar issue and while it is not exactly changing the namespace at run time it does all you to get objects etc from a different namespace at run time. If you want to be changing namespaces chances are you have classes with the same names and interfaces but different implementations otherwise your code would need to be changed. What you need to do if make a new lib and namespace that just contains the interfaces for all the classes you want to use. You then make the classes in the different namespaces inherit these interfaces so you can code your class to just use the interface not the particular implementation. Then to select the implementation to use at run time you use "Dependancy Injection" to choose the correct implementation to insert via config file or let some other part of your application configure the injection. Dependancy Injection can be a little hard to get started with but once you get your head around it, it will make your life a lot easier. Ninject is a nice easy dependancy injection framework to start with.

Resources