我正在为 Rails6 开发一个 Rails 引擎。我想在那个 Rails 引擎中包含 Stimulus 控制器。主应用程序使用 Webpacker。并希望将 grid_controller.js 移动到我的 rails 引擎中,因为它会自动包含在主应用程序中。
应用程序/javascript/packs/application.jsrequire("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
import { Application } from "stimulus"
import { definitionsFromContext } from "stimulus/webpack-helpers"
应用程序/javascript/controllers/grid_controller.js
import { Controller } from "stimulus"
export default class extends Controller {
sort(event) {
var urlParams = new URLSearchParams(location.search);
var attr = event.target.dataset.attr
var dir = urlParams.get("dir") || 'asc'
var currentAttr = urlParams.get("sort")
var currentURL = window.location.origin+window.location.pathname
if (attr == currentAttr){
dir = ((dir == 'asc') ? 'desc' : 'asc')
}
var gotoURL = currentURL+'?'+'sort='+attr+'&'+'dir='+dir
window.location.href = gotoURL
}
}