It is sometime you need to ask confirmation from user for something. Suppose your application is going to kill or save his girlfriend, you need to ask user whether or not he is fed up with her. In case he is, just execute the sequence of command that will call a bad terminator from future and terminate her, or else call a good terminator to save her. In the worst case, if she is really sucking, just find the program in the matrix that is responsible for her creation, and stop it’s execution.
I am not going to scribble the lines of code in the demo that do things which are mentioned above, instead, I left that opportunity to the user himself.
Let’s not just talk more, let’s create a new windows 8 JavaScript application. In the UI, we are going to have a button execute, and on the click of that button, we are going to ask user for a confirmation. Here is the HTML of my default.html page.
1: <body>
2: <div style="margin-left:200px;margin-top:100px;">
3: <button id="showMessageButton">Execute</button>
4: </div>
5: </body>
And in the JS file, that is, default.js, let’s create an event listener that will listen to the click event of the button.
1: app.onactivated = function (args) {
2: if (args.detail.kind === activation.ActivationKind.launch) {
3: if (args.detail.previousExecutionState !==
4: activation.ApplicationExecutionState.terminated) {
5: // adding a new event listener
6: showMessageButton.addEventListener("click", ShowMessage,false);
7: } else {
8: // TODO: This application has been reactivated from suspension.
9: // Restore application state here.
10: }
11: args.setPromise(WinJS.UI.processAll());
12: }
13: };
Now here comes the important part, the code of the ShowMessage function.
1: function ShowMessage() {
2: var msgpopup = new Windows.UI.Popups.MessageDialog("Are you fed up with your girlfriend?");
3: msgpopup.commands.append(new Windows.UI.Popups.UICommand("Yes", function () { }));
4: msgpopup.commands.append(new Windows.UI.Popups.UICommand("No", function () { }));
5: msgpopup.commands.append(new Windows.UI.Popups.UICommand("Sometimes she sucks", function () { }));
6: msgpopup.showAsync();
7: }
You can do anything after the button clicked in the inline function specified in each line above. This is the place where you can write that destructive code we talked before. To be clear, here is all the code of my default.js file.
1: // For an introduction to the Blank template, see the following documentation:
2: // http://go.microsoft.com/fwlink/?LinkId=232509
3: (function () {
4: "use strict";
5:
6: WinJS.Binding.optimizeBindingReferences = true;
7:
8: var app = WinJS.Application;
9: var activation = Windows.ApplicationModel.Activation;
10:
11: app.onactivated = function (args) {
12: if (args.detail.kind === activation.ActivationKind.launch) {
13: if (args.detail.previousExecutionState !==
14: activation.ApplicationExecutionState.terminated) {
15: // adding a new event listener
16: showMessageButton.addEventListener("click", ShowMessage,false);
17: } else {
18: // TODO: This application has been reactivated from suspension.
19: // Restore application state here.
20: }
21: args.setPromise(WinJS.UI.processAll());
22: }
23: };
24:
25: app.oncheckpoint = function (args) {
26: // TODO: This application is about to be suspended. Save any state
27: // that needs to persist across suspensions here. You might use the
28: // WinJS.Application.sessionState object, which is automatically
29: // saved and restored across suspension. If you need to complete an
30: // asynchronous operation before your application is suspended, call
31: // args.setPromise().
32: };
33:
34: app.start();
35:
36: function ShowMessage() {
37: var msgpopup = new Windows.UI.Popups.MessageDialog("Are you fed up with your girlfriend?");
38: msgpopup.commands.append(new Windows.UI.Popups.UICommand("Yes", function () { }));
39: msgpopup.commands.append(new Windows.UI.Popups.UICommand("No", function () { }));
40: msgpopup.commands.append(new Windows.UI.Popups.UICommand("Some time she sucks", function () { }));
41: msgpopup.showAsync();
42: }
43:
44:
45: })();
This is how a message box is displayed in an WinJS application. Stay tuned for more.
Happy Reading!!!
0 comments