davej / angular-classy

Cleaner class-based controllers with Angular 1
http://davej.github.io/angular-classy/
813 stars 27 forks source link

Classy config #7

Closed peterbe closed 8 years ago

peterbe commented 10 years ago

I'm currently faced with

.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
    $locationProvider.html5Mode(true);

    $routeProvider
    .when('/:owner/:repo', {
        templateUrl: "/partials/table.html",
        controller: 'PullsController'
    })
    .when('/', {
        templateUrl: "/partials/form.html",
        controller:'FormController'
    });
}])

This would look nicer as:

.classy.config({
    inject: ['$routeProvider', '$locationProvider'], 
    init: function () {
        this.$locationProvider.html5Mode(true);

        this.$routeProvider
        .when('/:owner/:repo', {
            templateUrl: "/partials/table.html",
            controller: 'PullsController'
        })
        .when('/', {
            templateUrl: "/partials/form.html",
            controller:'FormController'
        });
    }
});
crysfel commented 10 years ago

What about this?

.classy.routes({
    html5Mode      : true,

    '/:owner/:repo'  : {
        templateUrl: "/partials/table.html",
        controller: 'PullsController'
    },
    '/' : {
        templateUrl: "/partials/form.html",
        controller:'FormController'
    }
});

That looks cleaner to me xD

peterbe commented 10 years ago

It's certainly cleaner but it's braver and a higher abstraction. Dare we?

davej commented 10 years ago

Yeah, I think the abstraction is too high for the moment. Where possible I want to keep the Classy approach somewhat consistent with controllers. There are many things that you can do with config, not just routes. @crysfel's solution wouldn't support ui-router either (which is very popular). I think @peterbe's is the most likely solution.

Thanks guys, these API discussions are great. If either of you have API suggestions for directives (#1), filters(#2) or services (#3), I'd love to hear them. :)

Freyskeyd commented 10 years ago

I think @peterbe's is the most too.

It's cleaner to have an init and other method i think.

.classy.config({
    inject: ['$routeProvider', '$locationProvider'], 
    init: function () {
        this.$locationProvider.html5Mode(true);
        this._router();
    },

    _router: function () {
       // init router
    }
});