Command Pattern implementation for AngularJS.
bower install ng-command
ng-command
lets you write commands which can only execute once whilst executing and also if canExecute
is met.
var app = angular.module('commandLab', ['ng-command'])
.controller('CommandExampleCtrl', ['$command', '$scope', '$timeout',
function ($command, $scope, $timeout) {
var self = this;
self.saveCmd = $command($scope, save, canExecute);
function save (){
return $timeout( function() {
console.log("save", "yay!");
}, 2000);
}
function canExecute() {
return !self.isBusy;
}
<button class="btn" ng-disabled="!vm.saveCmd.canExecute" ng-click="vm.saveCmd.execute()">