Skip to content

Commit

Permalink
Finalised the controllerAs merge and added a test for the functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
dwmkerr committed Jan 23, 2015
1 parent 4614975 commit 6cbddb6
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 19 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-modal-service",
"version": "0.5.0",
"version": "0.6.0",
"homepage": "https://github.com/dwmkerr/angular-modal-service",
"authors": [
"Dave Kerr (github.com/dwmkerr)"
Expand Down
21 changes: 14 additions & 7 deletions dst/angular-modal-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

// Get the body of the document, we'll add the modal to this.
var body = $document.find('body');

function ModalService() {

var self = this;

// Returns a promise which gets the template, either
// from the template parameter or via a request to the
// from the template parameter or via a request to the
// template url parameter.
var getTemplate = function(template, templateUrl) {
var deferred = $q.defer();
Expand Down Expand Up @@ -52,17 +52,23 @@
};

self.showModal = function(options) {

// Create a deferred we'll resolve when the modal is ready.
var deferred = $q.defer();

// Validate the input parameters.
var controller = options.controller;
if(!controller) {
var controllerName = options.controller;
if(!controllerName) {
deferred.reject("No controller has been specified.");
return deferred.promise;
}

// If a 'controllerAs' option has been provided, we change the controller
// name to use 'as' syntax. $controller will automatically handle this.
if(options.controllerAs) {
controllerName = controllerName + " as " + options.controllerAs;
}

// Get the actual html of the template.
getTemplate(options.template, options.templateUrl)
.then(function(template) {
Expand All @@ -74,7 +80,7 @@
// the scope, as well as all inputs provided.
// We will also create a deferred that is resolved with a provided
// close function. The controller can then call 'close(result)'.
// The controller can also provide a delay for closing - this is
// The controller can also provide a delay for closing - this is
// helpful if there are closing animations which must finish first.
var closeDeferred = $q.defer();
var inputs = {
Expand Down Expand Up @@ -104,7 +110,8 @@
inputs.$element = modalElement;

// Create the controller, explicitly specifying the scope to use.
var modalController = $controller(controller, inputs);
var modalController = $controller(controllerName, inputs);


// Finally, append the modal to the dom.
if (options.appendElement) {
Expand Down
4 changes: 2 additions & 2 deletions dst/angular-modal-service.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dst/angular-modal-service.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-modal-service",
"version": "0.5.0",
"version": "0.6.0",
"description": "AngularJS Service for showing Modals and Popups",
"main": "server.js",
"scripts": {
Expand Down
16 changes: 9 additions & 7 deletions src/angular-modal-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,18 @@
var deferred = $q.defer();

// Validate the input parameters.
var controller = options.controller;
if(!controller) {
var controllerName = options.controller;
if(!controllerName) {
deferred.reject("No controller has been specified.");
return deferred.promise;
}

// If a 'controllerAs' option has been provided, we change the controller
// name to use 'as' syntax. $controller will automatically handle this.
if(options.controllerAs) {
controllerName = controllerName + " as " + options.controllerAs;
}

// Get the actual html of the template.
getTemplate(options.template, options.templateUrl)
.then(function(template) {
Expand Down Expand Up @@ -104,12 +110,8 @@
inputs.$element = modalElement;

// Create the controller, explicitly specifying the scope to use.
var modalController = $controller(controller, inputs);
var modalController = $controller(controllerName, inputs);

if(options.controllerAs) {

inputs.$scope[options.controllerAs] = modalController;
}

// Finally, append the modal to the dom.
if (options.appendElement) {
Expand Down
26 changes: 26 additions & 0 deletions test/controller.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ describe('controller', function() {
$scope.input1 = input1;
$scope.input2 = input2;
$scope.close = close;
})
.controller('ControllerAsController', function() {
this.character = "Fry";
});

beforeEach(function() {
Expand Down Expand Up @@ -71,4 +74,27 @@ describe('controller', function() {

});

it('should add a controller to the scope if controllerAs is used', function() {

$httpBackend.expectGET('some/controllertemplate.html');

ModalService.showModal({
controller: 'ControllerAsController',
controllerAs: 'futurama',
templateUrl: 'some/controllertemplate.html'
}).then(function(modal) {

// The controller should be on the scope.
expect(modal.scope.futurama).not.toBeNull();

// Fields defined on the controller instance should be on the
// controller on the scope.
expect(modal.scope.futurama.character).toBe('Fry');

});

$httpBackend.flush();

});

});

0 comments on commit 6cbddb6

Please sign in to comment.