davej / angular-classy

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

Disable the scope inclusion of all methods. #46

Closed fabulouslord closed 9 years ago

fabulouslord commented 9 years ago

Is this possible? I would like to prefix with _ the methods utilized by other public methods but don't expose this public ones as they get put directly into the scope, which is widely recognized as a bad practice.

E.g.

init : function(){
    this.$scope.qt = {
        user: user,
        getToken: this.getToken
    };
},
methods : {
    getToken : function(){
        // A method I don't want to put directly on the scope
    }
}

So instead of calling it like

<button ng-click="getToken()"></button>

I can call it like

<button ng-click="qt.getToken()"></button>

but currently the method would be still be put into the scope directly and I could call it either way, which I do not want to.

I hope I make sense, and look forward to a reply.

davej commented 9 years ago

I'm sorry I don't really understand your question. Methods prefixed with _ currently won't be put in the scope.

If you're worried about $scope pollution then you should look into the 'contoller as' syntax, it is compatible with Classy and will give you what I think you're looking for.

fabulouslord commented 9 years ago

What I mean is that I don't want non prefixed methods to be put into the scope as well, as they pollute the scope and I would like to put them into an object and expose the object instead, while still being able to utilize the underscore to denote private methods and non prefixed methods to denote public methods. What is it that "controller as" syntax? , couldn't find any reference.

Cheers.

davej commented 9 years ago

You can do this globally with:

myApp.classy.options.controller = {
  addToScope: false
};

or on a per-controller basis

myApp.classy.controller({
  name: 'TodoCtrl',
  inject: ['$scope', 'filterFilter'],
  __options: {
      addToScope: false
  }
  // ...
});

More info on controller-as here: http://toddmotto.com/digging-into-angulars-controller-as-syntax/ Also check out the FAQs here: http://davej.github.io/angular-classy/

davej commented 9 years ago

By the way if you only want to apply this behaviour to methods (and not the data object) then you could do this:

myApp.classy.options.controller = {
  bindMethods: {
    addToScope: false
  }
};

Does that answer all your questions?

fabulouslord commented 9 years ago

Yes it does Dave! Thanks for the quick feedback!