Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to let Angularjs work with prototype methods and variables

You know, in angularjs, most of logical are based on $scope:

function Ctrl($scope) {
    $scope.name = "Freewind";
    $scope.hello = function() {
       alert($scope.name);
    }
    $scope.method1 = function() {}
    $scope.method2 = function() {}
    $scope.method3 = function() {}
    $scope.method4 = function() {}
    $scope.method5 = function() {}
}

Now I'm using haxe to generate angularjs code, it works if my code is:

class Ctrl {
   public function new(scope:Scope) {
      scope.name = "Freewind";
      scope.hello = function() {
         alert(scope.name);
      }
      scope.method1 = function() {}
      scope.method2 = function() {}
      scope.method3 = function() {}
      scope.method4 = function() {}
      scope.method5 = function() {}
   }
}

typedef Scope = {
    name:String,
    hello:Void->Void,
    method1: Void->Void,
    method2: Void->Void,
    method3: Void->Void,
    method4: Void->Void,
    method5: Void->Void
}

But I want to be benefited from haxe's class system(the code may be simpler and clearer), to declare it like:

class Scope {
    public var name:String;
    public function hello() {}
    public function method1() {}
    public function method2() {}
    public function method3() {}
    public function method4() {}
    public function method5() {}
}

Then find a way to associate the Scope class with the $scope of angularjs.

But the generated Scope from haxe are using prototypes:

Scope = function();
Scope.prototype.name = "something";
Scope.prototype.hello = function() {}
Scope.prototype.method1 = function() {}
Scope.prototype.method2 = function() {}
Scope.prototype.method3 = function() {}
Scope.prototype.method4 = function() {}
Scope.prototype.method5 = function() {}

In this case, I can't find a solution to let angularjs work with it.

Is it possible to let angularjs to work with prototypes, so it can work with haxe class system (also other languages like coffeescript/typescript which have class support)?

like image 907
Freewind Avatar asked Oct 21 '22 19:10

Freewind


1 Answers

For the sake of having an actual answer to this question, one should mention that now this library solves the problem: https://github.com/freewind/HaxeAngularSupport ;)

like image 163
back2dos Avatar answered Oct 27 '22 18:10

back2dos