ex_admin 中的自定义模板类型 (Phoenix)

Custom template types in ex_admin (Phoenix)

我在后端使用 ex_admin 在 Phoenix 中构建了一个博客,我想覆盖我的博客 post 模板的一些默认输入。

我的 post 具有以下属性:

  schema "posts" do
    field :title, :string
    field :image, :string   # will store as URL, will reference external host like S3
    field :created_on, :datetime
    field :content, :text

    timestamps()
  end 

我想做以下事情:

  1. :title 字段保持为文本输入
  2. :image
  3. 创建上传者输入
  4. 保留 Phoenix 默认的多重下拉选择方案 created_on
  5. 为我的 :content 创建一个 Quill wysiwyg(基本上放入一个针对 :content 文本字段的脚本)

理想情况下,我想做这样的事情:

defmodule MyBlog.ExAdmin.Post do
    use ExAdmin.Register

    register_resource MyBlog.Post do

    end

    form post do
        inputs do
            input post, :title, type: :string
            input post, :image, type: :file
            input post, :created_on, type: :datetime
            input post, :content, type: :text
        end
    end

    # Add a javascript hook that fires here to target the :content input

end

:title:image:created_on 字段都显示文本输入...我注意到唯一改变文本字段的类型是 type: :password。有没有办法从 inputs do 列表中动态添加这些类型,或者我是否必须创建自定义模板并引用它?

首先,您通常不需要指定字段的类型。一次例外是 text 字段,因为文本和字符串字段在模式元数据中都被键入为字符串。

这应该适用于您的 javascript 表格:

form post do
   inputs do
        input post, :title, type: :string
        input post, :image, type: :file
        input post, :created_on, type: :datetime
        input post, :content, type: :text
    end
    javascript do
      form_javascript()
    end
end
def form_javascript, do: """
  $(document).ready(function() {
    // ... 
  });
"""

你真的不需要使用form_javascript功能。你可以直接嵌入就可以了

    javascript do
      """
      $(document).ready(function() {
         // ... 
      });
      """
    end

不过,我喜欢把 javascript 分开,尤其是当它相当长的时候。