goldfinch/loadmore 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

goldfinch/loadmore

Composer 安装命令:

composer require goldfinch/loadmore

包简介

Load more implementation for Silverstripe with front-end component

README 文档

README

Silverstripe Version Package Version Total Downloads License

Load more implementation ⏳ for Silverstripe with front-end component 🌀. Easy to use and customize.

Screenshot

Available Taz commands

If you haven't used Taz🌪️ before, taz file must be presented in your root project folder cp vendor/goldfinch/taz/taz taz

Extend model making it loadable

php taz loadable

Create loadable template

php taz make:loadable-template

Install

1. Install module

composer require goldfinch/loadable

2. Add key to your .env

Use Taz🌪️ to generate and add the key.

php taz generate:app-key

3. Generate config file

Use Taz🌪️ to generate the config. It will quickly lead you through the setup and take care of it for you.

php taz loadable

4. Make sure these meta tags are presented in your header

<meta name="csrf-param" content="authenticity_token">
<meta name="csrf-token" content="{$SecurityID}">

5. Implement JavaScript front-end component

via Silverstripe Requirements PHP

Requirements::javascript('goldfinch/loadable:client/dist/loadable.js');

via template require

<% require javascript('goldfinch/loadable:client/dist/loadable.js') %>

via ES6 module

import Loadable from '..../vendor/goldfinch/loadable/client/src/src/loadable-mod';
// import Loadable from '@goldfinch/loadable/client/src/src/loadable-mod'; // with alias

document.addEventListener('DOMContentLoaded', () => {
  new Loadable();
});
// vite.config.js
// * only if you use alias import above

import { defineConfig } from 'vite';

export default defineConfig(({ command, mode }) => {

  return {

    // ..

    resolve: {
      alias: [
        { find: '@goldfinch', replacement: fileURLToPath(new URL('./vendor/goldfinch', import.meta.url)) },
      ],
    },

    // ..

  };
});

6. Create a loadable template (FYI)

The Taz command in the third step above would create the loadable template for your model so you don't really need to do anything here. Just for your information, if you are curious about how the templates initiate:

All loadable templates are stored within a single folder templates/Loadable. The name should be the same as your targeted model's name.

Example:

Loadable template for app/Models/MyLoadableModel.php would be themes/my_theme/templates/Loadable/MyLoadableModel.ss

(❗) The content in each template must start with a tag that has data-loadable-list-item attribute which represents a single loadable item

<div data-loadable-list-item>
  <!-- my custom code goes here -->
</div>

Real-case example:

<a href="{$Link}" data-loadable-list-item>
  $Image
  <h3>$Title</h3>
</a>

Usage

To call the loadable area use one of the examples below for further customization

Method 1 (quick preview for test)

$LoadableAs(App\Models\MyLoadableModel)

Method 2 (basic)

<% with $LoadableWith(App\Models\MyLoadableModel) %>
  <div data-loadable-area>
    $List
    <div>
      $Action
    </div>
  </div>
<% end_with %>

Method 3 (fully customizable)

<% with $LoadableWith(App\Models\MyLoadableModel) %>
  <div data-loadable-area>
    <% with Data %>
    <div data-loadable-list data-loadable-remains="$CountRemains">
      <% loop List %>
        $loadableTemplate
      <% end_loop %>
    </div>
    <div>
      <button
        data-loadable-action
        data-loadable-params='{"search": "some search value"}'
        data-loadable-stock="{$LoadableObject}"
        data-loadable-substance="{$LoadableMethod}"
        data-loadable-substance-id="{$LoadableMethodID}"
        data-loadable-scroll-offset="100"
        data-loading="false"
        class="btn btn-primary"
        type="button"
      >
        <span class="d-none spinner-border spinner-border-sm" aria-hidden="true"></span>
        <span role="status">Load more<span data-loadable-remaning></span></span>
      </button>
    </div>
    <% end_with %>
  </div>
<% end_with %>

Method 4 (bridge - list through class method)

To use this method, we need to do a few more settings:

  1. Call loadable instance passing $ID and $Method
$LoadableAs(App\Models\ProjectCategory, $ProjectCategoryID, Projects)
$LoadableWith(App\Models\ProjectCategory, $ProjectCategoryID, Projects)
  • Projects in this example is a method in ProjectCategory model that returns DataList. It could be basic custom method or has_many/many_many/belongs_many_many
class ProjectCategory {
    
    // as relationship
    private static $belongs_many_many = [
        'Projects' => ProjectItem::class,
    ];

    // or as custom method
    public function Projects()
    {
        // return $this->Projects();
        return ProjectItem::get();
    }
}
  1. Add bridge to our config:
Goldfinch\Loadable\Loadable:
  loadable:
    App\Models\ProjectItem:
      initial_loaded: 10
      per_each_load: 10
      bridge:
        App\Models\ProjectCategory: Projects

Other options

Properties like initial_loaded, per_each_load can be declared in database instead. Using SomeConfig. If you use Taz🌪️ command php taz loadable, it will handle this setup for you anyway, but for your reference:

  1. We need to specify dbconfig in our config as shown in the example below
Goldfinch\Loadable\Loadable:
  loadable:
    App\Models\MyLoadableModel:
      dbconfig:
        App\Configs\MyConfig:
          initial_loaded: InitialLoaded
          per_each_load: PerEachLoad
  1. Based on the example above, our MyConfig class would look like this:
use JonoM\SomeConfig\SomeConfig;
use SilverStripe\ORM\DataObject;
use SilverStripe\View\TemplateGlobalProvider;

class MyConfig extends DataObject implements TemplateGlobalProvider
{
    use SomeConfig;

    private static $db = [
        'InitialLoaded' => 'Int(10)',
        'PerEachLoad' => 'Int(10)',
    ];
}

Sidenotes

manually extended model

If you don't want to use LoadableExtension extension, you can prepare your loadable model

use SilverStripe\ORM\DataList;
use SilverStripe\Control\HTTPRequest;

public static function loadable(DataList $list, HTTPRequest $request, $data, $config): DataList
{
    // apply some additional filtering to the list as needed

    return $list;
}

public function loadableTemplate()
{
    return $this->renderWith('Loadable/MyLoadableModel');
}

Events

Available JavaScript callback events

window.goldfinch.loadmore_before_callback = (action) => {
  console.log('loadmore before', action)

  let list = action.closest('[data-loadable-area]').children('[data-loadable-list]')

  // ..
}

window.goldfinch.loadmore_after_callback = (action) => {
  console.log('loadmore after', action)

  let list = action.closest('[data-loadable-area]').children('[data-loadable-list]')
  
  // ..
}

License

The MIT License (MIT)

goldfinch/loadmore 适用场景与选型建议

goldfinch/loadmore 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 354 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 09 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「silverstripe」 「loadmore」 「load-more」 「loadable」 「loadmore-silverstripe」 「loadable-silverstripe」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 goldfinch/loadmore 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 goldfinch/loadmore 我们能提供哪些服务?
定制开发 / 二次开发

基于 goldfinch/loadmore 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 354
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-27