Skip to content

Commit

Permalink
feat(hintController): set up initial repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Erin Altenhof-Long committed Jul 2, 2014
1 parent 7fc54cc commit 6f141d9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/bower_components
28 changes: 28 additions & 0 deletions controllerDecorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(function() {

'use strict';

/**
* Decorates $controller with a patching function to
* throw an error if DOM APIs are manipulated from
* within an Angular controller
*/
angular.module('ngHintController', []).
config(function ($provide) {
$provide.decorator('$controller', function($delegate, $injector) {
return function(ctrl, locals) {
if(angular.isString(ctrl)) {
match = expression.match(CNTRL_REG),
constructor = match[1],
identifier = match[3];
ctrl = controllers.hasOwnProperty(constructor)
? controllers[constructor]
: console.log('Not on window') || console.log('It is against Angular best practices to instantiate a controller on the window.');
}
var ctrlInstance = $delegate.apply(this, [ctrl, locals]);
return ctrlInstance;
}
});
});

}());
19 changes: 19 additions & 0 deletions controllerDecorator_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
describe('controllerDecorator', function() {
var $controller;

beforeEach(module('ngHintController'));

beforeEach(inject(function(_$controller_) {
$controller = _$controller_;
}));

it('should detect if a controller is instantiated on the window', function() {
spyOn(console, 'log');
var controllerMock = function() {
var element = document.createElement('a');
element.innerHTML = 'testValue';
};
var sampleControl = $controller(controllerMock);
expect(console.log).toHaveBeenCalledWith('It is against Angular best practices to instantiate a controller on the window.');
});
});
16 changes: 16 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = function(config) {
config.set({
frameworks: ['jasmine'],
files: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'controllerDecorator.js',
'controllerDecorator_test.js'
],
exclude: [
],
preprocessors: {
},
browsers: ['Chrome']
});
};

0 comments on commit 6f141d9

Please sign in to comment.