ninjatronic / angular-base64

Base64 conversion for AngularJS
Other
186 stars 113 forks source link

problems with 'this' being clobbered somehow (maybe from angulars injector) #2

Closed boxxxie closed 10 years ago

boxxxie commented 10 years ago

the 'this' ref in the lib kept being undefined for some reason. i couldn't figure out why but i noticed that there was nothing special about using 'this' in the lib anyway.

I have fixed the problem by using factory instead of constant and removing all uses of 'this'.

the API has not changed.

would you be interested in a PR?

ninjatronic commented 10 years ago

Hi @boxxxie

The reason that base4 is wrapped in a constant instead of a library is to allow it to be used during the config stage of an app. One of the main use cases is for people with simple applications to perform one-time configuration that will set the Authorization header on the $http client using $httpProvider, which couldn't be achieved if it was wrapped up into a factory.

I'd be interested in the changes that rip out any references to this, or that improve the project in any other way (I'm well aware it's not the tidiest encapsulation ever) but I'm not sure that moving into a factory is the best thing for it.

Cheers

boxxxie commented 10 years ago

For me, the this ref is being clobbered. So this is a bug.

I can't use decode.

I can refactor to have it use constants again, however it would require duplicate code.

Can you show me an example code for configuring something where the constants are needed?

ninjatronic commented 10 years ago

Hi @boxxxie

Yep, definitely a bug, no dispute there. I've not seen it before so if you could post a demo and stack trace that would be great too.

A simple usecase of $base64 during config would look like:

angular.module('app', ['base64']).config(['$httpProvider', '$base64', function($httpProvider, $base64) {
    $httpProvider.defaults.headers.get['Authorization'] = 'Basic '+ $base64.encode('username:password');
}]);

It's quite a common thing to do, and couldn't work if base64 was wrapped as a factory.

I use $base64 during the config phase in a different project. You can see here that it is used during the config phase of an app, and then again during the run phase. It would not be difficult to move that code into the run block, but I think that $base64 should be accessible during both the config and run phases for maximum flexibility. There is no need to restrict it.

I'm going to take a look and see if I can reproduce these this issues that you are having.

Cheers

ninjatronic commented 10 years ago

Hi @boxxxie

See if c5eced8851d45a1031dd5c3db58b37cdbfef9ea6 solves your problem :)

Pete

ninjatronic commented 10 years ago

Closing this: as far as I can tell it is fixed. Please reopen of you are still experiencing problems.

Thanks