> For the complete documentation index, see [llms.txt](https://bekoturko.gitbook.io/yazilim-gelistirme-wiki/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bekoturko.gitbook.io/yazilim-gelistirme-wiki/uygulama-yapisi-ve-katmanlari/uygulama-alani-application-domain/javascript.md).

# Javascript

### document.ready olayı

Sayfalara ait istemci dosyalarında `document.ready` olayı işleyicisi tanımlamak yerine ortak olay işleyici metot kullanılır.\
`document.ready` olayına abone olmak isteyen metotlar `$ol` metodu ile global listeye (`$oa`) eklenir.\
`global.js` dosyasında bu listedeki her metot document.ready olayına ekleniyor.

```javascript
//onload function array
var $oa=[];

//add function to onload array
function $ol(f){
        if(typeof(f)=='function'){
        $oa.push(f);
    }
}

$(function(){
    //send onload array to jQuery
    $($oa).each(function(i,item){
        $(item);
    });
});

//örnek abone metot
$ol(function(){ 
        $("#deactiveMembershipBtn").on('click',function(e){
        var deactiveMembership = $("#deactiveMembership");
        $('#DeactivateMembershipForm').trigger('reset');
        $.fancybox(deactiveMembership);
        e.preventDefault();
    });
});
```

### Dinamik oluşan içeriklerde istemci validasyonu kuralları

Sayfa yüklendikten sonra dinamik olarak oluşturulan formların istemci validasyon kuralları otomatik olarak oluşmuyor. Bunun için aşağıdaki yöntem kullanılıyor.

```javascript
//yeni eklenen kayıtlar için form validasyon kurallarını yeniden oluştur
function resetFormValidator(formId){
    $(formId).removeData('validator');
    $(formId).removeData('unobtrusiveValidation');
    $.validator.unobtrusive.parse(formId);
}
```

### İstemci kodundan Resource mesajlarına erişim

İstemci kodları kullanıcıya mesaj göstereceği zaman kullanılan mesajlar Resource ile uygulama tarafında tanımlıysa, bu mesajlar için Hidden input elemanları oluşturuluyordu ve istemci kodunda bu mesajlar kullanılıyordu.\
Bunun yerine artık Resource mesajlarını bir javascript arrayine yazan bir T4 template oluşturuldu.\
Kullanılacak tüm mesajların bir kerede javascript kodlarının erişimine hazır hale getirilmesi sağlandı. Hidden input kullanımı kaldırıldı.

Kullanım:\
İstemci kodlarında erişilmek istenen mesaj bu dosyaya eklenir.\
Aşağıdaki gibi kullanılır.

```javascript
//onay mesajı
var result = confirm(Messages.PostDeleteConfirm);
```

**Örnek T4 template:**

`messages.tt`

```csharp
<#@template debug="false" hostspecific="false" language="C#" #>
<#@assembly name="System.Core"#>
<#@assembly name="System.Web"#>
<#@assembly name="$(TargetDir)\Foo.Web.Library.dll"#>
<#@assembly name="$(TargetDir)\Foo.Resources.dll"#>
<#@import namespace="Foo.Web.Library.Resources"#>
<#@import namespace="Foo.Resources"#>
<#@import namespace="System.Linq"#>
<#@import namespace="System.Text"#>
<#@import namespace="System.Web"#>
<#@import namespace="System.Collections.Generic"#>
<#@output extension=".js"#>
var Messages = {
 MemberUnblockConfirm:'<#=HttpUtility.JavaScriptStringEncode(Messages.UserUnblockConfirm)#>',
 FriendDeleteConfirm:'<#=HttpUtility.JavaScriptStringEncode(Messages.FriendDeleteConfirm)#>',
 FriendRequestDeleteConfirm:'<#=HttpUtility.JavaScriptStringEncode(Messages.FriendRequestDeleteConfirm)#>',
 MemberBlockedConfirm:'<#=HttpUtility.JavaScriptStringEncode(Messages.UserBlockedConfirm)#>',
 ComplaintNotesRequired:'<#=HttpUtility.JavaScriptStringEncode(WebValidationMessages.NotesNotEmpty)#>',
 PostFilterResultNotFound:'<#=HttpUtility.JavaScriptStringEncode(Messages.PostFilterResultNotFound)#>',
 WrongDate:'<#=HttpUtility.JavaScriptStringEncode(Messages.WrongDate)#>',
 PostDeleteConfirm:'<#=HttpUtility.JavaScriptStringEncode(Messages.PostDeleteConfirm)#>',
 ChildDeleteConfirm:'<#=HttpUtility.JavaScriptStringEncode(Messages.ChildInfoDeleteConfirm)#>',
 UnbornChildDeleteConfirm:'<#=HttpUtility.JavaScriptStringEncode(Messages.UnbornChildInfoDeleteConfirm)#>'   
}
```

Bu dosyada değişiklik yapılıp kaydedildiğinde `messages.js` yeniden oluşturuluyor.

### İstemci kodundan Url bilgilerine erişim

İstemci tarafında kullanılması gereken Url bilgileri aşağıdaki partial dosyasında tanımlanır. Bu partial Layout dosyalarına eklenmelidir. Bu şekilde kullanılacak url bilgilerinin Routing yapısı tarafından oluşturulması sağlanır.**Yararları:**

* Gelecekte yapılabilecek Route tanımı değişiklikleri kullanılan url bilgilerini etkilemez.
* Url oluşturma işlemi merkezileştirilmiştir, kod tekrarı ve tekrara dayalı olası güncelleme sorunları yaşanmaz.

**Örnek Tanımlar:**

`~\Views\Shared\_Urls.cshtml`

```csharp
@using System.Web.Script.Serialization
@{
    var serializer = new JavaScriptSerializer();
    IDictionary<string, string> urls = new Dictionary<string, string>();
    urls.Add("ArticleOverviewList", Url.Action(ContentControllerActionNames.ArticleCategoryContentTeasers, ControllerNames.Content));
    urls.Add("ChildList", Url.Action(ProfileSettingsControllerActionNames.ChildList, ControllerNames.ProfileSettings));
    urls.Add("ContentMailForm", Url.Action(ContentMailControllerActionNames.PopupFormContentMail, ControllerNames.ContentMail));
    urls.Add("ContentPreview", Url.Action(ContentActionControllerActionNames.ContentPreview, ControllerNames.ContentAction));
    ...
}
<script type="text/javascript">
    var Urls = @Html.Raw(serializer.Serialize(urls));
</script>
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bekoturko.gitbook.io/yazilim-gelistirme-wiki/uygulama-yapisi-ve-katmanlari/uygulama-alani-application-domain/javascript.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
