I am working with Aurelia and Typescript and I'm trying to achieve the following thing: have a base class called Parent, extend this class in a class called Child and then inject an instance of Child in another class.
Here's the setup:
//file1
export class Parent {
constructor() {
debugger;
}
}
//file2
import {Parent} from "file1";
export class Child extends Parent {
constructor() {
super();
debugger;
}
}
//file3
import {inject} from "aurelia-framework";
import {Child} from "file2";
#inject(Child)
export class Home {
private child: Child;
constructor(_child: Child) {
this.child = _child;
debugger;
}
}
However, when I do this and instantiate Home, I get the following error:
Uncaught SyntaxError: Unexpected token <
along with ERROR [app-router] Router navigation failed, and no previous location could be restored.
Now, the first error, Uncaught SyntaxError: Unexpected token < gives me a reference to file1.js at the first line. (which strangely contains the html code from the index of the application).
Now, if I take the injection out of file3 and make something like this:
//#inject(Child)
export class Home {
private child: Child;
constructor() {
this.child = new Child(); //here I don't inject, I just
//instantiate a new object of type Child - still the same error
debugger;
}
}
I get exactly the same error, so it doesn't seem to be injection related.
So, how can I have a base class called Parent, extend this class in a class called Child and then inject an instance of Child in another class?
Or is something in my approach that is not right?
Thanks!
UPDATE: The simple fact of having a the call for a new Child() breakes everything, it doesn't matterr if it is called at the loading of the page, in the constructor, or if it is in a method on a button. It breakes when loading.
buttonMethod(){
var x = new Child(); //it breakes the same way
}
Now if I move the Child class in the same file as Home and file3 looks like this:
import {inject} from "aurelia-framework";
import {Parent} from "file1";
export class Home {
child: Child;
constructor() {
this.child = new Child();
debugger;
}
}
export class Child extends Parent {
constructor() {
super();
debugger;
}
}
and I instantiate it like this it works. However, when I try to inject it, so:
import {inject} from "aurelia-framework";
import {Parent} from "file1";
#inject(Child)
export class Home {
child: Child;
constructor(_child: Child) {
this.child = _child;
debugger;
}
}
export class Child extends Parent {
constructor() {
super();
debugger;
}
}
I get: inner error: Error: key/value cannot be null or undefined. Are you trying to inject/register something that doesn't exist with DI?
In the end I want to have them in separate files, but it is a start making it work so:)
Thanks!
Ok, so the Typescript compiler finds file1 because it is in the .csproj file so it doesn't need the full path, but at runtime, the Aurelia framework finds the file (after the typescript code is transpiled) something like localhost/file1.js. So you have 2 possibilities: either create a tsd.json in the typings folder (assuming you are using AMD module system) in which to set the absolute paths for your typescript definitions or always write the full path when importing custom typings.
Related
I'm new to dart. Currently, working on a mobile app through flutter. I have a Helper class which has some common methods which I've planned throughout the app. I've included that Helper class in another class. But, can't able to fig. out how to access its methods.
My commom Helper class code:
import 'dart:async';
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
class Helper {
Map userDetails = {};
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
// --- Method for getting user details from shared preference ---
Future<Map>getUserDetailsFromSharedPreference () async {
try {
final SharedPreferences prefs = await _prefs;
if(prefs.getString('user') != null) {
this.userDetails = json.decode(prefs.getString('user'));
} else {
print('Shared preference has no data');
}
} catch (e){
print('Exception caught at getUserDetails method');
print(e.toString());
}
return this.userDetails;
}
}
Here is my main program code where I've included the Helper class & trying to access it's getUserDetailsFromSharedPreference (). In this case, I'm getting an error like Only static memebers can be accessed in initializers. I also tried to extends Helper class in UserProfile class. But, there also I'm getting a different kind of errors. Can't able to identify how to do this thing.
import 'package:flutter/material.dart';
import 'helper.dart';
class UserProfile extends StatefulWidget {
#override
UserProfileState createState() => new UserProfileState();
}
class UserProfileState extends State<UserProfile> {
Helper helper = new Helper();
var userData = helper.getUserDetailsFromSharedPreference();
}
#Günter Zöchbauer I've made my Helper.dart file like this as you've suggested -
import 'dart:async';
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
class Helper {
Map userDetails = {};
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
static Helper _instance;
factory Helper() => _instance ??= new Helper._();
Helper._();
// --- Method for getting user details from shared preference ---
Future<Map>getUserDetailsFromSharedPreference () async {
try {
final SharedPreferences prefs = await _prefs;
if(prefs.getString('user') != null) {
this.userDetails = json.decode(prefs.getString('user'));
} else {
print('Shared preference has no data');
}
} catch (e){
print('Exception caught at getUserDetails method');
print(e.toString());
}
return this.userDetails;
}
}
Now, in my tryint to access that getUserDetailsFromSharedPreference() method I'm getting the same error Only static memebers can be accessed in initializers .
You could ensure a singleton instance of the class using a public factory constructor with a private regular constructor:
class Helper {
static Helper _instance;
factory Helper() => _instance ??= new Helper._();
Helper._();
...
}
If you call new Helper(), you'll always get the same instance.
You need to import the file that contains class Helper {} everywhere where you want to use it.
??= means new Helper._() is only executed when _instance is null and if it is executed the result will be assigned to _instance before it is returned to the caller.
update
getUserDetailsFromSharedPreference is async and can therefore not be used in the way you use it, at least it will not lead to the expected result. getUserDetailsFromSharedPreference returns a Future that provides the result when the Future completes.
class UserProfileState extends State<UserProfile> {
Helper helper = new Helper();
Future<Map> _userData; // this with ??= of the next line is to prevent `getUserDetailsFromSharedPreference` to be called more than once
Future<Map> get userData => _userData ??= helper.getUserDetailsFromSharedPreference();
}
If you need to access userData you need to mark the method where you do with async and use await to get the result.
foo() async {
var ud = await userData;
print(ud);
}
To access other class method you can simply put static on the method.
class Helper {
static printing(String someText){
print(someText);
}
}
void main() {
Helper.printing('Hello World!');
}
I think this question is more related to accessing one class data in another class. So I explained on the basis of my understanding of the question but if I'm not correct about it.
but if you want to access data of class A, not directly but through class B.
so first you have to make an object of A in class B but remember one thing you would have to make the object static in order to get access to the data of class A within Class B
If you still are confused about all this, I made a solution video.
Check it out: https://youtu.be/shK7ZraruCI
I'm perfectly willing to play with this until I get it right, but was hoping someone might give me a hint. The parameter is declared in the docs (gen-dartdocs/dart-mirrors/ClassMirror/newInstance.html) as
InstanceMirror newInstance(Symbol constructorName,
List positionalArguments,
[Map<Symbol,dynamic> namedArguments]);
There is a nice writeup on the format of positionalArguments and namedArguments in the docs. However, it is just a little on the abstract side of my current tolerance level.
A decent discussion also exists at
http://japhr.blogspot.com/2014/06/dart-factory-method-pattern.html
But, alas, no examples of actually passing args into the method.
In my case, I would like to simply pass two args, "title" and "description" into an unnamed subclass constructor.
Here's my code so far:
file: item.dart
import 'dart:mirrors';
abstract class Item {
String title;
String description;
factory Item(String type) {
MirrorSystem libs = currentMirrorSystem();
LibraryMirror lib = libs.findLibrary(new Symbol('app.models'));
Map<Symbol, Mirror> classes = lib.declarations;
// To do: handle exception if class not found
ClassMirror cls = classes[new Symbol(type)];
// TODO:
// verify each subclass has no-arg ctor
// determ how to pass args to ctor.
InstanceMirror inst = cls.newInstance(new Symbol(''), []);
return inst.reflectee;
}
// conflicts w/ Item factory
// Item(this.title, this.description);
}
And here's the class that gets instantiated:
file: model.dart
library app.models;
import 'item.dart' show Item;
/// The barebones model for a codelab. Defines constants used for validation.
class Codelab implements Item {
// ...
}
Finally, here is how the Item factory is called. ItemElement is the superclass of its own hierarchy, subclassed by CodelabElement:
file: item_element.dart:
import 'item.dart' show Item;
class ItemElement {
Item item;
final String itemType;
ItemElement() {
item = new Item(itemType);
}
// ...
}
And CodelabElement:
file: codelab_element.dart
import 'model.dart' show Codelab;
import 'item_element.dart' show ItemElement;
class CodelabElement extends ItemElement {
final itemType = "Codelab";
CodelabElement() : super() {}
//...
}
And then:
file: main.dart
void main() {
var element = new CodelabElement();
}
Currently, the new Codelab instance is returned from newInstance() (very cool), but it doesn't contain the inherited 'title' and 'description' attrs.
Maybe it has something to do with my being unclear on the usage of "extends" and "implements".
This should work
cls.newInstance(new Symbol(''), ['a', 1] /*,
{#arg1Name: 'arg1Value', #arg2Name: 'arg2Value'}*/ );
and is like
new MyClass('a', 1, arg1Name: 'arg1Value' /*, arg2Name: 'arg2Value'*/);
Just saw, Named arguments are not implemented.
You can try it in DartPad
Let us suppose the following situation. There is a global module AppModule, a scoped module ScopedModule, a class Main and a class Foo in an application's main variant. Moreover, there is a debug variant with a module DebugAppModule, a module DebugScopedModule and a class Bar. Only the debug variant may know about Bar.
The main variant contains the following relevant code excerpts.
#Module AppModule { /*..*/ }
#Module(injects=Main.class, addsTo=AppModule.class)
ScopedModule { #Provides Foo provideFoo() { return new Foo(); } }
class Main { scopedGraph = graph.plus(new ScopedModule(this)); }
class Foo { /*..*/ }
// In the entry point of the application
ObjectGraph.create(new AppModule());
The debug variant contains the following relevant code excerpts.
#Module(addsTo=AppModule.class, overrides=true) DebugAppModule { /*..*/ }
#Module(injects=Main.class, addsTo=DebugAppModule.class, overrides=true)
DebugScopedModule { #Provides Foo provideFoo() { return new Bar(); } }
class Bar extends Foo { /*..*/ }
// In the entry point of the application
ObjectGraph.create(new AppModule(), new DebugAppModule());
My research and experimentation revealed that it is not possible to override #Provides-methods in scoped modules, i.e. when plusing a module. See for example How to Mock Dagger Activity Object Graphs. That is, in the debug variant whenever a Foo is injected it still would be a Foo and not a Bar. This makes sense because the class Main has a fixed dependency to ScopedModule (note the new).
It seems to me that there should be a way to inject scoped modules themselves – meta-injection so to say :). That is, AppModule could provide ScopedModule for Main. The problem is that ScopedModule's constructor needs an instance of Main and so AppModule would need to retain an instance of Main and that would not fly (e.g. in an Android-specific context where Main would be an Activity).
So what is the best alternative way to achieve the effect of overriding #Provides-methods when using scoped modules?
With the latest version of Dagger, overriding #Provided methods are not permitted.
I found a good solution here. Thanks to #vaughandroid
Basically,
When you are providing your module into your component, you can override your methods.
MyComponent component = DaggerMyComponent.builder()
.appModule(new AppModule() {
#Override public Foo provideFoo() {
return new Bar();
}
})
.build();
This has worked for me and I guess it'll work for you.
I have a document class with this function on it, it targets a symbol with an instance name of scene4_headlights on the stage:
function accUpdate(e:AccelerometerEvent):void{
scene4_headlights.rotation += (e.accelerationX*10);
}
But I keep getting error 1120: Access of undefined property scene4_headlights even though I have a symbol on the stage with the instance name of scene4_headlights..
Help?
Here you go:
package {
import flash.sensors.Accelerometer;
import flash.events.AccelerometerEvent; //importing everything you need is cruicial
import flash.display.MovieClip;
public class CustomClassName extends MovieClip //your custom class can extend the type of class you used to create your scene4_headlights instance (some prefer using Sprite when the timeline is not required)
{
private var accelerometer = new Accelerometer(); //declare a variable data type: Accelerometer
public function CustomClassName() {
// constructor code
accelerometer.addEventListener(AccelerometerEvent.UPDATE, accUpdate); // add an eventlistener to the variable you just created
}
public function accUpdate(e:AccelerometerEvent): void //declare the function handling the eventlistener
{
scene4_headlights.rotation += (e.accelerationX*10);
trace("The function accUpdate is linked properly"); //trace is your friend
}
}
}
I tried to dynamically create a new instance of a class like this:
this.componentClass.newInstance(new Symbol(''), [this, el]).reflectee;
The class reflected in this.componentClass is called ButtonComponent and it is a subclass of Component. When running a test on this, I get an error:
Test failed: Caught No constructor 'ButtonComponent.' declared in class 'ButtonComponent'.
NoSuchMethodError : method not found: 'ButtonComponent.'
Receiver: Type: class 'ButtonComponent' Arguments: [...]
There are default constructors in both Component and ButtonComponent classes. Here is the code, to make sure I didn't miss anything:
class Component {
Element element ;
Template template;
Component(this.template, this.element) {
this.element.replaceWith(new Element.html(template.html));
}
}
class ButtonComponent extends Component {
ButtonComponent(template, element) : super(template, element) {};
}
Any ideas what is wrong here? Thank you.
I just made a similar test in 1.0.0.3_r30187 and I don't get this error. If you don't use the last stable version of Dart you should update your version.
Here's my tested code :
import 'dart:html';
import 'dart:mirrors';
class Component {
Element element ;
Component(this.element) {
this.element.children.add(new Element.html("<b>Dart rocks</b>"));
}
}
class ButtonComponent extends Component {
ButtonComponent(element) : super(element);
}
main() {
final a = reflectClass(ButtonComponent).newInstance(new Symbol(''),
[document.documentElement]).reflectee;
print(a); // display : Instance of 'ButtonComponent'
}