PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Bifrost.Web/Scripts/bifrost.commands.js

#
JavaScript | 94 lines | 79 code | 15 blank | 0 comment | 15 complexity | f52de5730d9585c0927016cfedc7c729 MD5 | raw file
 1var Bifrost = Bifrost || {};
 2
 3if (typeof ko === 'undefined') {
 4    throw "Requires Knockout.js";
 5}
 6
 7ko.bindingHandlers.command = {
 8    init: function (element, valueAccessor, allBindingAccessor, viewModel) {
 9        ko.applyBindingsToNode(element, { click: valueAccessor().execute }, viewModel);
10    }
11};
12
13
14(function () {
15    function S4() {
16        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
17    }
18    function guid() {
19        return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
20    }
21
22    function CommandDescriptor(name, commandParameters) {
23        this.Name = name;
24
25        var commandContent = {
26            Id: guid()
27        };
28
29        for (var parameter in commandParameters) {
30            if (typeof (commandParameters[parameter]) == "function") {
31                commandContent[parameter] = commandParameters[parameter]();
32            } else {
33                commandContent[parameter] = commandParameters[parameter];
34            }
35        }
36
37
38        this.Command = JSON.stringify(commandContent);
39    };
40
41    function Command() {
42        var self = this;
43        this.hasError = false;
44        this.isBusy = ko.observable();
45        this.canExecute = ko.observable(true);
46
47        this.execute = function () {
48            self.hasError = false;
49            if (self.beforeExecute) {
50                self.beforeExecute(self);
51            }
52
53            if (!self.canExecute()) {
54                return;
55            }
56            self.isBusy(true);
57
58            $.ajax({
59                url: "CommandCoordinator/handle",
60                type: 'POST',
61                dataType: 'json',
62                data: JSON.stringify(new CommandDescriptor(self.name, self.parameters)),
63                contentType: 'application/json; charset=utf-8',
64                error: function (e) {
65                    self.hasError = true;
66                    self.error = e;
67
68                    if (self.onError != undefined) {
69                        self.onError(self);
70                    }
71                },
72                complete: function () {
73                    if (!self.hasError) {
74                        if (self.onSuccess != undefined) {
75                            self.onSuccess(self);
76                        }
77                    }
78                    if (self.onComplete != undefined) {
79                        self.onComplete(self);
80                    }
81                    self.isBusy(false);
82                }
83            });
84        };
85    };
86
87    Bifrost.commands = {
88        create: function (configuration) {
89            var cmd = new Command();
90            $.extend(cmd, configuration);
91            return cmd;
92        }
93    };
94})();