JSFiddle - React, Tailwind, and code Playground
by vojtajina
JavaScript
function SomeController(scope, player, $resource) {
// private state
var some = 10;
var more = function() {};
// export stuff to scope
scope.img = 'default';
scope.player = player;
// export methods to scope
scope.calledFromTemplate = function(a, b) {
// this === scope that method is actually called on ???
// - need to access controller using "self" (required when using prototype definition of methods on controller)
// this === controller ? bound to controller instance ?
// - need to pass the actual scope as parameter
};
// watch, we don't need to pass scope anymore
scope.$watch('whatever', function(newValue, oldValue) {
scope.product = $resource().get();
});
}
// another syntax
function Other() {
this.$$privateMethod = function() {};
this.exportedToScope = function() {};
}
// UNIT TESTING
var controller;
inject(function($injector, $rootScope) {
controller = $injector.instantiate(SomeController, {scope: $rootScope});
// $provide.value('scope', $rootScope);
});
inject(function(scope) {
// do some stuff as it was called from tempalte
scope.calledFromTemplate('....');
scope.$digest();
// or access controller
controller.whathever()
expect(controller.x).toBe(y);
expect(scope.a).toBe(b);
});