How to properly use javascript namespaces in / PartialView

I played with MVC for a while, but as the project I'm on starts to fall in its sails, more and more people are added to it. Since I am responsible for hacking to learn some “best practice”, I am especially wary of possible javascript abuse and would like to know what would be the best way for our views and partial views to play well with javascript.

At the moment, we have a code that looks like this (only simplified, for example, for the sake of)

<script type="text/javascript">
    function DisableInputsForSubmit() {
        if ($('#IsDisabled').is(':checked')) {
            $('#Parameters :input').attr('disabled', true);
        } else {
            $('#Parameters :input').removeAttr('disabled');
        }
    }
</script>

<%=Html.SubmitButton("submit", Html.ResourceText("submit"), New With {.class = "button", .onclick = "DisableInputsForSubmit(); if ($('#EditParameters').validate().form()) {SetContentArea(GetHtmlDisplay('SaveParameters', 'Area', 'Controller'), $('#Parameters').serialize());} return false;"})%><%=Html.ResourceIcon("Save")%>

Here we save the form and send it to the server, but we turn off the input, which we do not want to check if the check box is selected.

some context

  • , Html.Resource *,
  • SetContentArea ajax GetHtmlDisplay URL- , .
  • combres, , , javascript

, - DisableInputsForSubmit (, javascript), .

(Resig jQuery Google javascript) /. , . ? i:

  • ? , - , .
  • javascript / , javascript , ? , javascript /?
  • , , - ?

, , JS, , , :)

+3
1

/ . , javascript onclick, , js :

<script type="text/javascript">
(function() {
    // your button selector may be different
    $("input[type='submit'].button").click(function(ev) {
        DisableInputsForSubmit();

        if ($('#EditParameters').validate().form()) {  
            SetContentArea(GetHtmlDisplay('SaveParameters', 'Area','Controller'), $('#Parameters').serialize());
        } 
        ev.preventDefault();
    });

    function DisableInputsForSubmit() {
        if ($('#IsDisabled').is(':checked')) {
            $('#Parameters :input').attr('disabled', true);
        } else {
            $('#Parameters :input').removeAttr('disabled');
        }
    }
})();
</script>

, .

:

, , . - :

(function() {

    MyNS = MyNS || {};

    MyNS.DisableInputsForSubmit = function() {
        //yada yada
    }

})();
+3

Source: https://habr.com/ru/post/1764700/


All Articles