ruby-on-rails - How require only a channel that I need?
问题描述
All channels are created on folder javascript/channels
and by default, all inner this path is loaded.
To not require all I just delete the file channels/index
that contains:
// Load all the channels within this directory and all subdirectories.
// Channel files must be named *_channel.js.
const channels = require.context('.', true, /_channel\.js$/)
channels.keys().forEach(channels)
But how require my channels at pages that I need?
Using = javascript_pack_tag "channels/my_channel"
at pages that I need doest work.
Which are is the best practices? How keep many channels and require only where I need?
解决方案
频道 js 文件(确切地说是频道订阅者)通常包含类似
const channel_subscription = consumer.subscriptions.create({
channel: 'SomeChannel',
received: function(data) {...}
});
订阅在执行时创建(默认在页面加载时),但您可以将其设为函数:
function connect_this_exact_channel() {
return consumer.subscriptions.create(...)
}
并且仅在调用该函数时才会创建订阅。
推荐阅读
- mysql - 为什么我不能从 aws lambda 访问数据库,但可以从具有相同登录数据的本地计算机访问?
- vba - 将附件保存到新的 Windows 文件夹?
- c# - 我的数据库集
从单元测试 (MSTest) 代码尝试时不会更新 - python - Flask-WTForms 使用 jQuery 移动到下一个表单字段
- python - 按层次结构从许多字典中获取值
- java - 美化函数中的异常处理
- javascript - 来自 JSON API 的数据表视图数据
- r - 如何在R中以一年中的一天为y轴和年份为x轴绘制日历日期?
- javascript - Javascript 帮助(初学者)
- java - JPA 外键 OneToMany