Dokümanlar /
... / View'lar / Backend JavaScript

💻 Backend JavaScript (Sunucu Tarafında Çalıştırma)

Backend JavaScript, sunucuda şablon render etme sırasında JavaScript kodu çalıştırmanıza olanak tanır. Bu kod, HTML tarayıcıya gönderilmeden önce çalışır.

Temel Kullanım

<script:odac>
  // This runs on the SERVER during template rendering
  let total = 0;
  for (let item of cart) {
    total += item.price * item.quantity;
  }
</script:odac>

<p>Total: $<odac var="total" /></p>

Temel Özellikler

  • ✅ Şablon render etme sırasında sunucuda çalışır
  • ✅ Tüm backend değişkenlerine ve Odac nesnesine erişebilir
  • ✅ Hesaplamalar, veri işleme ve filtreleme için idealdir
  • ✅ Tam IDE sözdizimi vurgulama ve otomatik tamamlama sunar
  • ❌ Tarayıcıda ÇALIŞMAZ
  • ❌ Tarayıcı API'lerine (window, document, localStorage vb.) erişemez

Backend JavaScript Ne Zaman Kullanılır

Backend JavaScript'i şunlar için kullanın:

  • Hesaplamalar: Toplamlar, ortalamalar, istatistikler
  • Veri dönüştürme: Dizileri filtreleme, sıralama, eşleme
  • Karmaşık mantık: Satır içi koşullar için fazla karmaşık olan mantık
  • Değişken hazırlama: Görüntüleme için geçici değişkenler oluşturma

Değişkenlere Erişim

Controller'da tanımlanan tüm değişkenlere erişebilirsiniz:

// Controller
module.exports = async function(Odac) {
  Odac.set('products', [
    { name: 'Laptop', price: 999, quantity: 2 },
    { name: 'Mouse', price: 29, quantity: 5 }
  ])
  
  Odac.View.skeleton('main').set('content', 'cart')
}
<!-- View -->
<script:odac>
  let total = 0;
  let itemCount = 0;
  
  for (let product of products) {
    total += product.price * product.quantity;
    itemCount += product.quantity;
  }
  
  const avgPrice = total / itemCount;
</script:odac>

<div class="cart-summary">
  <p>Total Items: <odac var="itemCount" /></p>
  <p>Total Price: $<odac var="total" /></p>
  <p>Average Price: $<odac var="avgPrice.toFixed(2)" /></p>
</div>

Odac Nesnesine Erişim

Odac nesnesine ve tüm metotlarına tam erişim:

<script:odac>
  const isLoggedIn = Odac.Auth.check();
  const currentUser = isLoggedIn ? Odac.Auth.user() : null;
  const requestMethod = Odac.Request.method;
  const currentUrl = Odac.Request.url;
</script:odac>

<odac:if condition="isLoggedIn">
  <p>Welcome, <odac var="currentUser.name" />!</p>
</odac:if>

Pratik Örnekler

Alışveriş Sepeti Hesaplamaları

<script:odac>
  let subtotal = 0;
  let totalItems = 0;
  
  for (let item of cart) {
    subtotal += item.price * item.quantity;
    totalItems += item.quantity;
  }
  
  const tax = subtotal * 0.18; // 18% tax
  const shipping = subtotal > 100 ? 0 : 10;
  const total = subtotal + tax + shipping;
</script:odac>

<div class="cart-summary">
  <h3>Order Summary</h3>
  <p>Items (<odac var="totalItems" />): $<odac var="subtotal.toFixed(2)" /></p>
  <p>Tax (18%): $<odac var="tax.toFixed(2)" /></p>
  <p>Shipping: 
    <odac:if condition="shipping === 0">
      <span class="free">FREE</span>
    <odac:else>
      $<odac var="shipping.toFixed(2)" />
    </odac:if>
  </p>
  <hr>
  <p class="total">Total: $<odac var="total.toFixed(2)" /></p>
</div>

Filtreleme ve Sıralama

<script:odac>
  // Filter active products
  const activeProducts = products.filter(p => p.isActive && p.stock > 0);
  
  // Sort by price
  activeProducts.sort((a, b) => a.price - b.price);
  
  // Get featured products
  const featured = activeProducts.filter(p => p.featured).slice(0, 3);
  
  // Calculate statistics
  const avgPrice = activeProducts.reduce((sum, p) => sum + p.price, 0) / activeProducts.length;
  const maxPrice = Math.max(...activeProducts.map(p => p.price));
  const minPrice = Math.min(...activeProducts.map(p => p.price));
</script:odac>

<div class="products-section">
  <h2>Featured Products</h2>
  <p>Showing <odac var="featured.length" /> of <odac var="activeProducts.length" /> products</p>
  <p>Price range: $<odac var="minPrice" /> - $<odac var="maxPrice" /></p>
  
  <odac:for in="featured" value="product">
    <div class="product">
      <h3><odac var="product.name" /></h3>
      <p>$<odac var="product.price" /></p>
      
      <odac:if condition="product.price < avgPrice">
        <span class="badge">Great Deal!</span>
      </odac:if>
    </div>
  </odac:for>
</div>

Tarih ve Saat Biçimlendirme

<script:odac>
  const now = new Date();
  const postDate = new Date(post.createdAt);
  
  // Calculate time difference
  const diffMs = now - postDate;
  const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
  const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
  const diffMinutes = Math.floor(diffMs / (1000 * 60));
  
  let timeAgo;
  if (diffDays > 0) {
    timeAgo = diffDays + ' days ago';
  } else if (diffHours > 0) {
    timeAgo = diffHours + ' hours ago';
  } else if (diffMinutes > 0) {
    timeAgo = diffMinutes + ' minutes ago';
  } else {
    timeAgo = 'Just now';
  }
  
  const formattedDate = postDate.toLocaleDateString('en-US', {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  });
</script:odac>

<div class="post">
  <h2><odac var="post.title" /></h2>
  <p class="meta">
    Posted <odac var="timeAgo" /> (<odac var="formattedDate" />)
  </p>
</div>

Veri Gruplama

<script:odac>
  // Group products by category
  const grouped = {};
  for (let product of products) {
    if (!grouped[product.category]) {
      grouped[product.category] = [];
    }
    grouped[product.category].push(product);
  }
  
  // Sort categories
  const categories = Object.keys(grouped).sort();
</script:odac>

<div class="products-by-category">
  <odac:for in="categories" value="category">
    <div class="category-section">
      <h2><odac var="category" /></h2>
      <p><odac var="grouped[category].length" /> products</p>
      
      <odac:for in="grouped[category]" value="product">
        <div class="product">
          <h3><odac var="product.name" /></h3>
          <p>$<odac var="product.price" /></p>
        </div>
      </odac:for>
    </div>
  </odac:for>
</div>

Sayfalama Mantığı

<script:odac>
  const itemsPerPage = 10;
  const currentPage = parseInt(Odac.Request.get('page')) || 1;
  const totalItems = products.length;
  const totalPages = Math.ceil(totalItems / itemsPerPage);
  
  const startIndex = (currentPage - 1) * itemsPerPage;
  const endIndex = Math.min(startIndex + itemsPerPage, totalItems);
  const currentItems = products.slice(startIndex, endIndex);
  
  const hasPrevious = currentPage > 1;
  const hasNext = currentPage < totalPages;
</script:odac>

<div class="products">
  <odac:for in="currentItems" value="product">
    <div class="product">
      <h3><odac var="product.name" /></h3>
    </div>
  </odac:for>
</div>

<div class="pagination">
  <odac:if condition="hasPrevious">
    <a href="?page=<odac var="currentPage - 1" />">Previous</a>
  </odac:if>
  
  <span>Page <odac var="currentPage" /> of <odac var="totalPages" /></span>
  
  <odac:if condition="hasNext">
    <a href="?page=<odac var="currentPage + 1" />">Next</a>
  </odac:if>
</div>

Karmaşık Koşul Mantığı

<script:odac>
  const user = Odac.Auth.check() ? Odac.Auth.user() : null;
  
  const canEdit = user && (
    user.role === 'admin' || 
    user.id === post.authorId
  );
  
  const canDelete = user && user.role === 'admin';
  
  const canComment = user && !user.isBanned && post.commentsEnabled;
  
  const showActions = canEdit || canDelete || canComment;
</script:odac>

<div class="post">
  <h2><odac var="post.title" /></h2>
  <p><odac var="post.content" /></p>
  
  <odac:if condition="showActions">
    <div class="actions">
      <odac:if condition="canEdit">
        <a href="/posts/<odac var="post.id" />/edit">Edit</a>
      </odac:if>
      
      <odac:if condition="canDelete">
        <a href="/posts/<odac var="post.id" />/delete">Delete</a>
      </odac:if>
      
      <odac:if condition="canComment">
        <a href="#comments">Add Comment</a>
      </odac:if>
    </div>
  </odac:if>
</div>

Birden Fazla Script Bloğu

Aynı view içinde birden fazla <script:odac> bloğu kullanabilirsiniz:

<script:odac>
  let total = 0;
</script:odac>

<odac:for in="items" value="item">
  <div><odac var="item.name" /></div>
  
  <script:odac>
    total += item.price;
  </script:odac>
</odac:for>

<p>Total: $<odac var="total" /></p>

İstemci Tarafı JavaScript ile Karşılaştırma

Backend JavaScript (<script:odac>):

<script:odac>
  // Runs on SERVER during rendering
  const total = products.reduce((sum, p) => sum + p.price, 0);
</script:odac>
<p>Total: $<odac var="total" /></p>

İstemci Tarafı JavaScript (<script>):

<script>
  // Runs in BROWSER after page loads
  document.addEventListener('DOMContentLoaded', function() {
    console.log('Page loaded');
    
    // Can access browser APIs
    localStorage.setItem('visited', 'true');
    
    // Can manipulate DOM
    document.querySelector('.button').addEventListener('click', function() {
      alert('Clicked!');
    });
  });
</script>

En İyi Uygulamalar

  1. Basit tutun: Karmaşık mantık controller'larda olmalıdır
  2. Hesaplamalar için kullanın: Toplamlar, ortalamalar, filtreleme için idealdir
  3. Ağır işlemlerden kaçının: Veritabanı sorguları veya API çağrıları yapmayın
  4. Anlamlı değişken adları kullanın: Kodu kendi kendini açıklayan hale getirin
  5. Gerektiğinde yorum ekleyin: Karmaşık hesaplamaları açıklayın

İyi:

<script:odac>
  const discountedPrice = product.price * (1 - product.discount / 100);
  const savings = product.price - discountedPrice;
</script:odac>

Kaçının:

<script:odac>
  // Don't do this - should be in controller
  const users = await Odac.DB.users.get();
  const apiData = await fetch('https://api.example.com/data');
</script:odac>

Yaygın Kullanım Senaryoları

  • ✅ Toplam ve ara toplamları hesaplama
  • ✅ Dizileri filtreleme ve sıralama
  • ✅ Tarih ve sayıları biçimlendirme
  • ✅ Verileri gruplama ve toplulaştırma
  • ✅ Geçici görüntüleme değişkenleri oluşturma
  • ✅ Basit koşul mantığı
  • ❌ Veritabanı sorguları (controller kullanın)
  • ❌ API çağrıları (controller kullanın)
  • ❌ Dosya işlemleri (controller kullanın)
  • ❌ Ağır hesaplamalar (controller kullanın)