app factory ModalHelper modal function modal var baseInjectables scope

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
app.factory('ModalHelper', ['$modal', function($modal) {
var baseInjectables = ['$scope', '$modalInstance', '$saveForm', 'resourceAction', 'model'];
return function(templateUrl, additionalResolves) {
return new function(){
var self = this;
function ModalController($scope, $modalInstance, $saveForm, resourceAction, model) {
$scope.model = model;
// copy resolves to the scope
var resolves = [];
for (var i = baseInjectables.length; i < arguments.length; i++) {
resolves.push(arguments[i]);
}
Object.keys(self.updatedAdditionalResolves).forEach(function(resolveName, index) {
$scope[resolveName] = resolves[index];
});
$scope.cancel = function() {
return $modalInstance.dismiss('cancel');
};
$scope.save = function() {
$saveForm($scope, resourceAction.call(null, $scope.model).$promise, function (savedObject) {
$modalInstance.close(savedObject);
});
};
}
this.openModal = function(model, resourceAction, updatedResolves) {
var injectablesForController = baseInjectables.slice();
self.updatedAdditionalResolves = angular.extend(additionalResolves || {}, updatedResolves || {});
Object.keys(self.updatedAdditionalResolves).forEach(function(resolveName) {
injectablesForController.push(resolveName);
});
injectablesForController.push(ModalController);
var resolveMap = angular.extend({
resourceAction: function() { return resourceAction; },
model: function() { return model; }
}, self.updatedAdditionalResolves);
return $modal.open({
templateUrl: templateUrl,
resolve: resolveMap,
controller: injectablesForController
})
}
};
};
}]);