Live data from Hacker News

Classy: Cleaner class-based controllers for AngularJS

davej.github.io

11–20 of 59 posts

Re: Classy: Cleaner class-based controllers for AngularJS

#11
There is really no need for this. It adds nothing imo but another layer of sauce over what angular already does properly.

If your controllers are getting huge, go refactor into directives and services. I don't see how this would help since there isn't even inheritance?

also:

    app.controller('AppCtrl', ['$scope', '$location', '$http'], function($scope, $location, $http) {
    // ... 
    }]);
vs

    app.classy.controller({ name: 'AppCtrl', inject: ['$scope', '$location', '$http'],
      //...
    });
Congratulations, you have saved absolutely nothing (10 bytes? ) and just made it harder for me to understand your angular app.

One of the brilliant things about angular is that it provides a proper structure that anyone that needs to write angular needs to adhere to. If you start introducing layers upon layers of funkyness and glitter, you'll end up with something non-angular.

what I mean by that is: Classy introduces new conventions in a frameworks that already has a lot of conventions to keep track of while coding, and my biggest argument against this is that you introduce a dependency to Classy for all code based upon it, therefore you've created an extra layer of complexity to fix and track down when angular changes.

In my mind effort like this should go into the angular core to make it better.

Re: Classy: Cleaner class-based controllers for AngularJS

#14
I like this. One thing I like about is it how it sets up a canonical place to init things.

The other nice effect is the use of the injector instead of the repeated list of dependencies.

When not using coffeescript in angular, I could see making good use of this. Thanks !

Re: Classy: Cleaner class-based controllers for AngularJS

#15

There is really no need for this. It adds nothing imo but another layer of sauce over what angular already does properly. If your controllers are getting huge, go refactor into directives and services. I don't see how this would help since there isn't even inheritance? also: app.controller('AppCtrl', ['$scope', '$location', '$http'], function($scope, $location, $http) { // ... }]); vs app.classy.controller({ name: 'A…

Thanks for the feedback. I would argue that the Classy example is more expressive and crucially it is DRY.

Edit:

> One of the brilliant things about angular is that it provides a proper structure that anyone that needs to write angular needs to adhere to

Angular doesn't provide structure for controllers, they are just javascript functions. If you want to add structure it is up to the individual developer to decide how to do it. Classy is just the way that I like to do it, Classy is opinionated so it won't be for everyone.

By the way, you can do inheritance but I haven't documented and fully tested it yet.

Re: Classy: Cleaner class-based controllers for AngularJS

#16
post #9

Typescript classes work nicely with Angular controller I find. Coffeescript ones too.

Afaik you cant declare anonymous classes in typescript,let alone wrapping them in a closure.

No problem with Coffeescript.

    module.controller 'MainCtrl', class
        constructor:($scope,bar,baz)->
          $scope.message="foo"
        @$inject = ['$scope','bar','baz']
that's why i prefer the later.types or not.

Re: Classy: Cleaner class-based controllers for AngularJS

#17

I like this. One thing I like about is it how it sets up a canonical place to init things. The other nice effect is the use of the injector instead of the repeated list of dependencies. When not using coffeescript in angular, I could see making good use of this. Thanks !

It works really well with Coffeescript too. In fact it was written with Coffeescript and internally it uses Coffeescript's `Class` syntax.

It was originally inspired by this gist: https://gist.github.com/elado/8138516

Re: Classy: Cleaner class-based controllers for AngularJS

#18
post #15

There is really no need for this. It adds nothing imo but another layer of sauce over what angular already does properly. If your controllers are getting huge, go refactor into directives and services. I don't see how this would help since there isn't even inheritance? also: app.controller('AppCtrl', ['$scope', '$location', '$http'], function($scope, $location, $http) { // ... }]); vs app.classy.controller({ name: 'A…

Thanks for the feedback. I would argue that the Classy example is more expressive and crucially it is DRY. Edit: > One of the brilliant things about angular is that it provides a proper structure that anyone that needs to write angular needs to adhere to Angular doesn't provide structure for controllers, they are just javascript functions. If you want to add structure it is up to the individual developer to decide ho…

Angular's dependency injection is still a matter of discussion and a lot of people are not completely happen with it. It'll also be made into something different for angular 2.0.

Still, besides that: DRY is IMO not about these 2 lines that you've refactored away here. It's about whole functions/classes that have similar functionality where you're repeating yourself.

Therefore, this would be a micro-optimisation with negative results on impact on performance most likely (since there's more overhead)

Re: Classy: Cleaner class-based controllers for AngularJS

#19
post #7

Interesting project--I've done a little bit of Angular development and would be curious to look at this the next time I come back to Angular. As a side note, I am always impressed and insanely jealous of the nice websites that frontend libraries make for themselves. It makes sense since obviously folks with more frontend experience are writing the libraries, but I wish there were nice templates or tools to make such…

In relation to your side note, perhaps a site like http://html5up.net/ might be of use. I'd be interested if others have other similar resources they use.

Re: Classy: Cleaner class-based controllers for AngularJS

#20
post #10

I've experienced the problem of unwieldy controllers myself, but angular is already such an intricate, opinionated framework that I can't imagine adding another layer with its own DSL. Good code organization solves a lot of these problems.

I was about tu post the same comment. I can't imagine maintaining a code using angular and another layer on top that adds another syntax on top of scopes, bindings, and controllers. Sorry to whoever did that lib for the critic. That was obviously a big task and it really is full of good intention.

No need to apologise. I created the library for myself first and foremost, it's opinionated and it won't suit everybody but I'm ok with that. :)
Post reply on HN