برنامه نویسی

به زودی: JavaScript برای توسعه دهندگان ریل

این مقاله در ابتدا در طراح ریل منتشر شد


اواخر سال گذشته نشستم و به برخی از آمار سایتم نگاه کردم. من مقالات زیادی داشتم که هر ماه توسط افراد 5 رقمی خوانده می شود. در کنار آن ، من در dev.to (+30k) و یک مقدار محکم از مشترکان ایمیل قابل توجهی دارم. از تمام مقالاتی که می نویسم ، مواردی که در JavaScript بیشترین سؤالات را ایجاد می کنند: “اگر بخواهم این را می خواهم؟” ، “چگونه می توانید آن را برطرف کنید؟” و غیره

بنابراین ، پس از صحبت در مورد آن با چند دوست توسعه دهنده ، من کتابی را از قبل اعلام کردم: “JavaScript برای توسعه دهندگان ریل”. این یک ایده کاملاً جدید نبود ، من چند سال است که آن را در لیست ایده های خود کم کردم ، اما اکنون موضوعی از انواع آن نیز داشتم: JavaScript را به زبان دومین مورد علاقه خود تبدیل کنیدبشر من یک شکار داشتم چیزی بود بازار علاقه مند بود، اما من 100 ٪ مطمئن نبودم.

بنابراین من یک صفحه با وعده کتاب و گزینه ای برای پیش سفارش آن با تخفیف بزرگ ایجاد کردم. از آن زمان تعداد خوبی از شما کتاب را از قبل سفارش داده اید. آنقدر که من اطمینان داشتم که این کتاب باید به واقعیت تبدیل شود (من برای خودم هدف تعیین کردم و اگر همسان نباشم ، همه را بازپرداخت می کردم).

بنابراین بعد از اینکه تصمیم گرفتم با کتاب بگذرانم ، وقت زیادی را صرف یافتن رویکرد درست کردم. نوشتن کتاب به دور از مقالات محکم و محکم است که در اینجا می نویسم. اما در همین مورد ، مقالات دستی ؛ ساختن چیزی ملموس ، همیشه بهتر از انتزاع تر عمل می کرد.

بنابراین بعد از شروع بسیاری از کاذب (نه واقعاً: بسیاری!) ، من در نهایت با یک ایده حل و فصل کردم: ساختن یک ویرایشگر کد در محرک ، بر اساس Codemirrorبشر این ممکن است سطحی به نظر برسد ، اما من آن را تنظیم کردم تا با اصول اولیه شروع کنم: استفاده از وابستگی شخص ثالث در محرک تا گسترش عملکرد با هر فصل. سپس هر زمان که چیز جدیدی معرفی شود ، کاوش می کنم.

در اینجا گزیده ای وجود دارد (Markdown/Asciidoc واقعی که می نویسم):

    ()

    Now you got a super basic editor working that is not much better than running `data:text/html, ` in your browser. The content on load is static and any change made is lost when you refresh the page.

    ## 3.3.1 Passing data to a Stimulus controller

    Stimulus has the values API that allows to pass data to the controller from the HTML. Like a lot of things in Rails, Stimulus also follows certain conventions. You can pass values, like strings, integers and hash and array-objects using the following syntax: `data-[controllerName]-[valueName]-value=""`.

    Then in the controller you define it using the `static values` object, like this:

    ```

javascript
    import { Controller } from "@hotwired/stimulus"
    import { basicSetup } from "codemirror"
    import { EditorView } from "@codemirror/view"

    export default class extends Controller {
        static values = { content: String }

    // …
    }


    ```

    ### Static class field in JavaScript

    Wait static values object? You can compare the *static class field* to Ruby's class methods (`self.` or `⁠class << self`). It defines methods that belong to the class itself rather than instances of the class.

    In Ruby:

    ```

javascript
    class Example
    def self.class_method
        "It me!"
    end
    end

    Example.class_method # => "It me!"


    ```

    In JavaScript:

    ```

javascript
    class Example {
    static classMethod() {
        return "It me!"
    }
    }

    Example.classMethod() // "It me!"


    ```


    Both look pretty much alike, right? As an aside: in a later chapter, I will dive deeper into JavaScript classes.


    Stimulus adds some extras to it too; “hey, these properties should be available across all instances of this controller (not just the current instance). Oh… also I want you to automatically create methods to get/set these values.”

    So with the above added `static values = {}` line, you automatically get:

    1. Getters, like ⁠`this.contenValue`;
    2. Setters, like ⁠`this.contenValue = 42`;
    3. Has-methods, like ⁠`this.hasContentVale`.

    (…)

    ### Fat-arrow functions

    But before continuing, there is some interesting, and often confusing, JavaScript tomfoolery going on here that trips up many Ruby developers:

    ```

javascript
    EditorView.updateListener.of((update) => {
    // …
    })


    ```

    What you see here is called a (fat) arrow function `() => {}`. It was introduced in ES6/ES2015. In Ruby you can compare it to the  ⁠`->` (lambda) syntax, but with the added benefit of a lexical `this` binding. Lexical-what? It will automatically keep `this` pointing to where it was defined. Before ES6/ES2015, you had to add `bind(this)`. Let's change the above code to use the post-ES6 syntax to check it out:

    ```

javascript
    // …
    connect() {
        this.editor = new EditorView({
        doc: this.contentValue,
        parent: this.element,
        extensions: [
            basicSetup,
            // EditorView.updateListener.of((update) => {
                        // if (update.docChanged) { this.#update() }
                    // })
            EditorView.updateListener.of(function(update) {
            if (update.docChanged) { this.#update() }
            }.bind(this))
            ]
        })
        }
    // …


    ```

    When making changes in the editor you will still notice the console output with `Update` logs. Let's now remove `bind(this)` and try updating the editor's content again.

    ```

javascript
    // …
        connect() {
        this.editor = new EditorView({
        doc: this.contentValue,
        parent: this.element,
        extensions: [
            basicSetup,
            // EditorView.updateListener.of((update) => {
                        // if (update.docChanged) { this.#update() }
                    // })
            EditorView.updateListener.of(function(update) {
            if (update.docChanged) { this.#update() }
            })
        ]
        })
        }
    // …


    ```

    Errors! If you use a modern editor, you might already see a warning: `#update is declared but its value is never read`.

    Without using `bind(this)` or the (fat) arrow function, `this` would be the CodeMirror editor's context/scope because that is what is calling the function. So `this.#update` fails: CodeMirror doesn't have an `#update` method, only the *editor_controller* does. Compared to Ruby it is akin to losing the `self` reference inside a block.

    ()
حالت تمام صفحه را وارد کنید

از حالت تمام صفحه خارج شوید

این رویکرد مفید در ساختن چیزی واقعی و مفید است ، در حالی که یادآوری (جدید ، مدرن) جاوا اسکریپت را به صورت مرحله به مرحله. من می دانم که این کار بسیار بهتر از پیشبرد مفاهیم JavaScript در خلاصه است.

من در حال حاضر در حال ویرایش کتاب هستم. رفع تایپی ، حذف قطعات و بهبود بیت ها. در حالی که من هرگز در مورد چیزی که ایجاد کرده ام مطمئن نبودم ، اما از نحوه معلوم شدن این کتاب نیز واقعاً خوشحالم.

قیمت گذاری

قیمت گذاری آزمایشی اینگونه به نظر می رسد:

  1. توسعه دهنده انفرادی (49 دلار) – کتاب کامل (PDF/EPUB) ؛ دسترسی کامل به پایه کد ؛ قطعه کد
  2. حرفه ای (69 دلار) – همه چیز در توسعه دهنده انفرادی ؛ ویرایشگر MarkDown + نوع پیش نمایش ؛ دسترسی به جامعه Railsdesigners.com (راه اندازی تابستان 2025 ؛ ارزش 49 دلار!)
  3. تیم (149 دلار) – همه چیز در حرفه ای ؛ با 5 توسعه دهنده به اشتراک بگذارید. 5 کرسی جامعه

اگر شما یک توسعه دهنده ریل بوده اید که در JavaScript نفرین کرده است ، این کتاب ممکن است برای شما باشد. اگر شما یک توسعه دهنده ریل به هر قیمتی از JavaScript اجتناب کرده اید ، این کتاب همان چیزی است که شما نیاز دارید. برای توسعه دهندگان ریل که آرزو داشتند JavaScript فقط ناپدید شوند ، این کتاب ممکن است نظر شما را تغییر دهد.

تاریخ انتشارات آزمایشی برای توسعه دهندگان JavaScript برای ریل آوریل 2025 است (بله ، هنوز هم جایی برای تفسیر باقی می گذارد). اگر می خواهید هنگامی که می توانید دست خود را به دست بیاورید ، به خبرنامه مشترک شوید (ممکن است هنگام راه اندازی یک کد تخفیف ارسال کنم. چه کسی می داند!).

JavaScript را برای توسعه دهندگان ریل بررسی کنید.

نوشته های مشابه

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *

دکمه بازگشت به بالا