-1

I'm trying to change the standard constructor to add myConstructor.

Based on the code here I coded the following statements:

@CustomTag('main-app')
class MainApp extends PolymerElement {

  factory MainApp.custom() {
    MainApp = new Element.tag('main-app');
    myConstructor();
  }
MainApp.created() : super.created();
void myConstructor() {
    print("myConstructor Started");
  }

Unfortunately the Editor shows some problems:
The Statement MainApp = new Element.tag('main-app') shows the error:
A Value of Type 'Element' cannot be assigned to a variable of type 'Type'.

The Statement myConstructor() shows the Error:
"Instance members cannot be accessed from a factory constructor"

anyone has a suggestion how I can solve the errors?

EDIT: The current code is looking like following:

@CustomTag('main-app')
class MainApp extends PolymerElement {

  @observable int counter = 0;


      factory MainApp.custom() {
    MainApp mainApp = new Element.tag('main-app');
    mainApp.myConstructor();
    return mainApp;
  }

  /// Constructor used to create instance of MainApp.
  MainApp.created() : super.created();

MainApp ma = new MainApp.custom();

  void myConstructor() {
    print("myConstructor Started");
    counter = 1;
  }

The following error is showing up:

Exception: type 'HtmlElement' is not a subtype of type 'MainApp' of 'mainApp'.
  MainApp.MainApp.custom    
  MainApp.MainApp.created

EDIT Version 2:

@CustomTag('main-app')
class MainApp extends PolymerElement {

  Controller controller = new Controller();

  @observable int counter = myConstructor();
  @observable int totalGlypto = 0;

  /// Constructor used to create instance of MainApp.
  MainApp.created() : super.created();


    static int myConstructor() {
    print("myConstructor Started");
    return controller.getTotal();
  }

  void setupCheckIn(Event e, var detail, Node target) {
    print("Setup started");
    controller.checkInGlypto();
    counter = controller.getTotal();
  }

  void resetCheckIn(Event e, var detail, Node target) {
    print("reset started");
  }

  void loadCheckIn(Event e, var detail, Node target) {
    print("load started");
    controller.loadData();
    counter = controller.getTotal();
  }


}

the code can be downloaded here

Community
  • 1
  • 1
ken
  • 471
  • 9
  • 19

2 Answers2

0

You are using the class MainApp like a variable. Change the following line

MainApp = new Element.tag('main-app');

to either

MainApp mainApp = new Element.tag('main-app');

or

var mainApp = new Element.tag('main-app');
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • I have changed the code as suggested:
    factory MainApp.custom() { MainApp mainApp = new Element.tag('main-app'); mainApp.myConstructor();} void myConstructor() { print("myConstructor Started");}
    but it seems myConstructor() is not executed.
    – ken Jan 03 '15 at 15:39
  • And, does it work as expected? You should return the value from the constructor as well. Otherwise there is no point in creating such a factory constructor. – Günter Zöchbauer Jan 03 '15 at 15:40
  • Unfortunatelly not, I have posted the new code under EDIT above. It looks like the myConstructor is not called. Thanks – ken Jan 03 '15 at 15:59
  • Do you call `new MainApp. custom()` anywhere? (Your factory constructor still doesn't return the instance.) – Günter Zöchbauer Jan 03 '15 at 16:04
  • I have now added the return instance and called the constructor (see the new code under EDIT) but there is still a problem with new MainApp.customer() as I should pass a parameter. – ken Jan 03 '15 at 16:25
  • What is that parameter for? Doesn't seem to make sense there. – Günter Zöchbauer Jan 03 '15 at 16:28
  • I agree so I have removed the parameter from the factory but then an Exception is occurring (see code above) – ken Jan 03 '15 at 16:33
  • `MainApp ma = new MainApp.custom();` is outside of any function/method - this can't work, how do you actually call this in your code. – Günter Zöchbauer Jan 03 '15 at 16:37
  • If you call this from main you should take a look at http://stackoverflow.com/a/20982658/217408 – Günter Zöchbauer Jan 03 '15 at 16:42
  • I have pushed the whole code to GitHub the link is above. I'm going to look at your link now – ken Jan 03 '15 at 16:51
  • The way you built your app you don't even need a custom constructor. Why do you want to add one anyway. I figured `MainApp ma = new MainApp.custom();` is a field but this is redundant within your element because it is the same as `this` – Günter Zöchbauer Jan 03 '15 at 16:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68142/discussion-between-ken-and-gunter-zochbauer). – ken Jan 03 '15 at 18:17
  • Sorry, I have to leave. Tomorrow I'll have time again. – Günter Zöchbauer Jan 03 '15 at 18:19
  • I have added a Getter to the code and it seems to work, I do not know exactly why but it seems to work fine – ken Jan 04 '15 at 09:50
0

ok, now I think I found an answer, the key is the following code:

@observable int get counter => myConstructor();
ken
  • 471
  • 9
  • 19