CommonJS: require class and extend class - class

I'm a bit struggling with the JavaScript classes (CommonJS).
I've got a class in a javascript 'module' which I can import to another js file using:
DucoMaster.js:
class DucoMaster {
constructor(node){
this.node = node;
}
}
module.exports = {DucoMaster}
DucoModules.js:
const {DucoMaster} = require("./DucoMaster");
...
let test = new DucoMaster;
console.log(test);
It builds and printing the test works, it prints the object as defined as class in DucoMaster.
Now I would like to import the module and use it to extend another class like:
'class DucoMaster extends ParentClass' within DucoModules.js
Is this possible with DucoModules.js?
Best regards,

Related

Angular 2 Base Class Output EventEmitter doesn't get raised or handled

Very simple base class Closer
import {EventEmitter, Output} from 'angular2/core';
export class Closer {
#Output() closed: EventEmitter<any> = new EventEmitter();
constructor() {}
close() {
this.closed.emit({});
}
}
If I extends Closer another class and then mark it up with
<derived-class (closed)="closeHandler()"></derived-class>
closeHandler() will never get called. I can see Closer.close() getting called but the emit doesn't get propagated by the derived class or handled by the class whose template contains the derived class and event binding.
If I simply move the #Output to the derived class it works. But it would seem that Angular2 should be putting that on derived classes. The ability to fully define a set of behavior and inherit it would be nice.
I had the same problem this morning and just figured out how to do it. All you have to do is add outputs: ['closed'] to the components section of the derived class that is extending Closer and you have to remove #Output() from the closed declaration inside the Closer class
Here is a quick demo based on your example:
Derived Class:
#Component({
selector: 'derived-class',
outputs: ['closed']
})
export class DerivedClass extends Closer {}
Closer Class:
import {EventEmitter} from 'angular2/core';
export class Closer {
closed: EventEmitter<any> = new EventEmitter();
constructor() {}
close() {
this.closed.emit({});
}
}

AS3 accessing stage from class

Timeline code:
import as3.first;
first._this=this;
var str1:String='this is timeline';
Class code:
package as3 {
import flash.display.MovieClip;
public class first extends MovieClip {
public static var _this:Object;
trace(_this.str1);
}
}
Error message:
TypeError: Error #1009: Cannot access a property or method of a null
object reference.
Trying to wrap my mind around how classes work. Need to access timeline functions and variables from class. What am I doing wrong and how can I make this work?
All in all what you are doing is somewhat weird. May be, you just want a document class for your SWF root? You as well could add a class to any movieclip in your library: both ways grant you access to timeline.
package as3
{
import flash.display.MovieClip;
public class first extends MovieClip
{
public static var _this:Object;
trace(_this.str1); // you may place code here... but consider this:
// this area is STATIC, the code here
// executes only once when class gets initialized,
// so, this happens BEFORE you assign first._this=this;
}
}

TypeScript 0.9.1 CommonJS: correctly declaring exported ambient class implementing external interface?

EDIT:
Put another way the following within a .d.ts file shouldn't produce compiler error TS2137 'Class "MyClass" does not implement interface "IInterface"':
interface IInterface {
someMethod():void;
}
declare module "mod" {
export class MyClass implements IInterface {
constructor();
}
}
because I'm not (and can't in a declaration) implementing anything. Is this a bug in the compiler or is there some other way/syntax to do what the above implies? I would think the compiler smart enough to know to precisely include IInterface's signature as part of MyClass, and not require its methods be redeclared.
ORIGINAL:
I'm trying to write a d.ts for the node component bunyan. Having a problem with exporting a class that implements an external interface, specifically RingBuffer which extends node's EventEmitter. The problem simplified is (below in a bunyan.d.ts file):
// this interface declared in <reference..., put inline here for simplicity
interface IExternal {
inheritedMethod():void;
}
interface RingBuffer extends IExternal {
write():void;
}
declare var RingBuffer: {
new():RingBuffer;
}
declare module "bunyan" {
export var RingBuffer;
}
then used in myNodeApp.js
/// <references path="bunyan.d.ts" />
import bunyan = require( 'bunyan' );
var rb = new bunyan.RingBuffer();
// compiler doesn't error on this; thinks RingBuffer is type any.
// also, no intellisense to show write() method.
rb.badFunc();
changing bunyan.d.ts to:
declare module "bunyan" {
export class RingBuffer { constructor(); }
}
compiles, but same problem when used; no intellisense, no compile errors.
changing bunyan.d.ts to
declare module "bunyan" {
export var RingBuffer:RingBuffer;
}
causes compile error in myNodeApp.js
// error TS2083: Invalid 'new' expression
import rb = new bunyan.RingBuffer();
removing from bunyan.d.ts
declare module "bunyan" {
...
}
causes compile error in myNodeApp.js
// error TS2071: Unable to resolve external module ''bunyan''
import bunyan = require( 'bunyan' );
changing bunyan.d.ts
interface IExternal {
inheritedMethod():void;
}
interface IRingBuffer extends IExternal {
}
declare module "bunyan" {
export class RingBuffer implements IRingBuffer {}
}
cause compile error
// error TS2137: Class "bunyan".RingBuffer declares interface IRingBuffer but
// does not implement it: type '"bunyan".RingBuffer' is missing property
// 'inheritedMethod' from type 'IRingBuffer'
implying I have to redeclare all inherited methods from all extended interfaces, besides IRingBuffer, which seems a bit ridiculuous to have to do in a d.ts file
Does anyone know the 'correct' way to declare an ambient class that implements an interface for consumption in another CommonJS module??
An alternate way to define it would be the way Jquery's typescript definition is defined. You have separate interfaces for static and instance members. Here is a sample complete definition:
interface IExternal {
inheritedMethod():void;
}
interface IRingBuffer extends IExternal {
write():void;
}
// Static functions and constructors
interface IRingBufferStatic{
new():IRingBuffer;
}
declare var RingBuffer:IRingBufferStatic;
declare module "bunyan" {
export var RingBuffer:IRingBufferStatic;
}
// In the second file
import bunyan = require( 'bunyan' );
var rb = new bunyan.RingBuffer();
// you get an error here
rb.badFunc();
Try it online

TypeScript module and class simultaneously?

I am currently trying to build a TypeScript definition file for OpenLayers.
The problem is that in OpenLayers there are certain classes that would translate to both a module and a class in TypeScript.
For example there is the Protocol class in module OpenLayers and there is a class Response in module OpenLayers.Protocol.
How could I model that in TypeScript? Can I make Protocol a class and define the Response class as a inner class that is exported? How would you go about solving that problem?
Declare Response as a static field of Protocol with a constructor type, returning an interface that defines the Response class:
declare module OpenLayers {
export interface IProtocolResponse {
foo(): void;
}
export class Protocol {
static Response: new () => IProtocolResponse;
}
}
var response = new OpenLayers.Protocol.Response();
response.foo();
Edit:
Or as Anders points out in this discussion list question, you can have multiple constructors for the inner class in this way:
declare module OpenLayers {
export interface IProtocolResponse {
foo(): void;
}
export class Protocol {
static Response: {
new (): IProtocolResponse;
new (string): IProtocolResponse;
};
}
}
var response = new OpenLayers.Protocol.Response('bar');
response.foo();
The main downside of both approaches is that you cannot derive a class from OpenLayers.Protocol.Response.
Here is my updated answer, which I hope helps - it should get you started on defining OpenType:
declare module OpenType {
export class Protocol {
constructor();
Request;
}
}
var x = new OpenType.Protocol();
var y = new x.Request();
var z = x.Request;

can doctrine generate model classes with prefix?

I am have modular structure in my project
--application
----modules
-----admin
------models
-----client
------models
Now when I generate the models and database using doctrine ( ./doctrine build-all-reload
).
it genrate the classes
class Car extends BaseCar
{ }
abstract class BaseCar extends Doctrine_Record
{ // code removed }
for zend to pick up these classess
I have to rename them like this
class admin_Model_Car extends admin_Model_generated_BaseCar
{ }
abstract class admin_Model_generated_BaseCar extends Doctrine_Record
{ // code removed }
Is there a way that I can genate the classes with prefix MODULENAME_MODEL_CLASSNAME?
So I don't need to change it.
Add these lines in application.ini file / module.ini file.
doctrine.generate_models_options.pearStyle = true
doctrine.generate_models_options.generateTableClasses = false
doctrine.generate_models_options.generateBaseClasses = true
doctrine.generate_models_options.baseClassPrefix = "Base_"
doctrine.generate_models_options.baseClassesDirectory =
doctrine.generate_models_options.classPrefixFiles = false
doctrine.generate_models_options.classPrefix = "admin_Model_"