Home Immediately-Invoked Function Expression
Post
Cancel

Immediately-Invoked Function Expression

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(function ($) {
    var model = {
        init: function () {
        }
    },
        view = {
            init: function () {
                model.init();
            }
        },
        controller = {
            init: function () {
                view.init();
            }
        };
    controller.init();
}(window.jQuery));

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
(function ($) {
    var model = {
        init: function () {
        }
    },
        view = {
            init: function () {
                model.init();
                this.$btnSendCode = $('#btnSendCode').click(this.btnSendCodeClick);
                this.$txtUsername = $('#Username');
                this.$modal = $('#modal').on('shown.bs.modal', this.reAlignModal);
                this.$modalBodyText = this.$modal.find('.modal-body');
                $(window).on("resize", function () {
                    $(".modal:visible").each(view.reAlignModal);
                });
                $("#modalTemplate").template("modalTemplate");
            },
            $txtUsername: null,
            $btnSendCode: null,
            btnSendCodeClick: function () {
                var mn = view.$txtUsername.val();
                controller.sendCodeRequest(mn);
            },
            $modal: null,
            $modalHeader: null,
            $modalBodyText: null,
            modalShow: function (dataObject) {
                $.tmpl("modalTemplate", dataObject).appendTo(this.$modalBodyText.empty());
                this.$modal.modal('show');
            },
            reAlignModal: function () {
                var $modalDialog = $(this).find(".modal-dialog");
                $modalDialog.css("margin-top", Math.max(0, ($(window).height() - $modalDialog.height()) / 3));
            }
        },
        controller = {
            init: function () {
                view.init();
            },
            sendCodeRequest: function (mobileNumber) {
                var postData = {
                    mobileNumber: mobileNumber
                };
                addAntiForgeryToken(postData);
                $.ajax({
                    cache: false,
                    type: "POST",
                    url: "@Url.Action("SendReminder", "CustomCustomer")",
                    data: postData,
                    dataType: "json",
                    success: controller.sendCodeResponseSuccess,
                    error: controller.sendCodeResponseError
                });
            },
            sendCodeResponseSuccess: function (data, textStatus, jqXHR) {
                if (data.isDistributorCustomer) {
                    view.modalShow({
                        distributor: {
                            name: data.distributorCompanyName,
                            email: data.distributorEmailAddress,
                            phone: data.distributorPhoneNumber,
                            hasName: data.distributorCompanyName !== null
                        }
                    });
                } else {
                    view.modalShow({
                        isSent: true
                    });
                }

            },
            sendCodeResponseError: function (jqXHR, textStatus, errorThrown) {
                if (jqXHR.responseJSON == null || jqXHR.responseJSON.Error == null)
                    return;

                view.modalShow({
                    error: jqXHR.responseJSON.Error
                });
            }
        };
    $(document).ready(controller.init);
}(window.jQuery));

1
2
3
4
5
6
7
8
9
10
11
12
13
// CSRF (XSRF) security
function addAntiForgeryToken(data) {
    //if the object is undefined, create a new one.
    if (!data) {
        data = {};
    }
    //add token
    var tokenInput = $('input[name=__RequestVerificationToken]');
    if (tokenInput.length) {
        data.__RequestVerificationToken = tokenInput.val();
    }
    return data;
};
This post is licensed under CC BY 4.0 by the author.