how to install easydropdown with Rails 6 - ruby-on-rails

What is the correct way to install easydropdown into Rails 6?
I ran yarn add easydropdown to add it to my app. Seems ok; I verified that my browser sees it in the js sources.
After this, not totally sure what to do. In app/javascripts/packs/application.js, I've tried adding these lines. None cause errors.
require('easydropdown')
import easydropdown from 'easydropdown'
import Easydropdown from 'easydropdown' - does case matter?
But in my page and js console, I keep seeing easydropdown is not defined.
What am I missing?

When using webpack the require statements are not global so you need to require the library in the file in which you are using it.
In your application.js pack file add the following:
import easydropdown from 'easydropdown';
document.addEventListener("turbolinks:load", () => {
easydropdown('.easydropdown')
});
as long as your selects have class of "easydropdown" this should make them all work.
Your styles need to be added either in your assets or imported in this pack file as well.

Thanks to #ben-trewern for helping me get here.
For Rails 6, here's what works for me:
yarn add easydropdown to install it into your Rails App
Append loader code to app/javascripts/packs/application.js.
I've found 2 ways that work. I honestly don't know which is better or why.
Option 1: using import
import easydropdown from 'easydropdown';
window.easydropdown = easydropdown;
Option 2: using require
const easydropdown = require('easydropdown')
window.easydropdown = easydropdown.default
Now you can use easydropdown functions in your views (including <script> tags).
Bonus help: Are you using Stimulus JS?
If your dropdown is connected to a Stimulus controller, here's some tips:
I found that I still had to assign window.easydropdown (above) in order to call EDD in my controller code.
Seems the most straightforward thing to do is to call easydropdown.select('#my-select') in my Stimulus controller's initialize() method, e.g.
export default class extends Controller {
static targets = [...]
initialize() {
easydropdown.select('#region-switcher`);
...
}
...

Related

Css issues when using ag-grid in more than a view in our react app

We are using the inside one of our react components which we called "GridViewWrapper" in which i've made all the required css et js ag-grid's related imports (i will share below how i did the imports).
Actually, Since a long time we've been using that "GridViewWrapper" only in one view in the whole app without any problem but then we needed to reuse it somewhere else in the app (in a different view which means that in the runtime that component will never be instanciated more than once in the same time because both views won't be shown together in the same time too)
Now, since i've started to used our "GridViewWrapper" in two views of our app , we started to have css issues in the ag-grid (some looks to be broken , for example checkboxes can't get checked anymore). It looks like there is a mess in the order of loading css files and some css might be overriden by another. https://i.imgur.com/jE8vcSv.gif
When i get started using the ag-grid in our react app , i don't remember i've seen any required webpack specific config for the ag-grid or specifically for typescript (which we are using it in our app too). But now i found this SO thread which talks about a webpack config https://stackoverflow.com/a/56552750/1705922 (they're using react too) and i also found this ag doc also mentionning a webpack config https://www.ag-grid.com/javascript-data-grid/building-typescript/.
Should we implement that in our react app too ?
Here is how i made the css et js ag-grid related files :
import { LicenseManager, RowNode } from "ag-grid-enterprise";
import {
GridApi,
Column,
RowClassParams,
PasteStartEvent,
PasteEndEvent,
CellValueChangedEvent,
CellEditingStoppedEvent,
CellEditingStartedEvent,
ColumnMovedEvent,
ColumnResizedEvent,
DragStoppedEvent,
GridReadyEvent,
RowDragEvent,
} from "ag-grid-community";
import "ag-grid-community/dist/styles/ag-grid.css";
import "ag-grid-community/dist/styles/ag-theme-alpine.css";
import { AgGridReact } from "ag-grid-react";
Are we missing something ?
These are the ag-grid verions in the package.json
"ag-grid-community": "^26.2.0",
"ag-grid-enterprise": "^26.2.0",
"ag-grid-react": "^26.2.0",

Vuex on global window instance

Ok so this is a pretty complicated question but I appreciate any help.
I have vue webpacker running on a rails application, I would like to use Vuex on it however all Vue components are unrelated, a solution I found online (here) was someone who added Vuex to the global window instance and imported that javascript into his main layout, I have done so and can see properly that a Vuex instance exists on my global window instance.
The code for that javascript I'm running is this:
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
const vuex_store = new Vuex.Store({
strict: true,
});
window["vue"] = Vue;
window["vuex_store"] = vuex_store;
Now my question is, in an unrelated vue component also running on my rails server, how would I import and use this Vuex instance?
Used it as a bus directly as I wanted initially.
window.vue.$on and window.vue.$emit anywhere

Rails: Accessing JS module methods from files served by webpacker

Context
I try to move assets in our application to webpack using Webpacker gem. Application is very big, so I need to do it partially.
What did so far...
I successfully managed to call the script using javascript_pack_tag
I export a super simple module:
# javascript/src/javascript/test.js'
const Direction = {
log_to_console: function(){
console.log('test');
}
};
export default Direction;
Then import it in the application entry point
# javascript/packs/application.js
import Test from '../src/javascript/test.js'
Test.log_to_console();
Finally rendering it in the layout:
# app/views/application.slim
= javascript_include_tag 'application'
The result is: "Test" string visible in the browser console.
The problem
Currently in the whole application we use Modules in views like this:
# app/views/assets/javascripts/test.coffee
log_to_console = ->
console.log('test');
#Test = { log_to_console }
# app/views/some/template.slim
javascript:
Test.log_to_console()
But after moving module to webpack I can't access the Test module anymore.
So my question is:
How to configure webpacker gem OR refactor the code above to make the log_to_console() method available in views/browser inspector?
Ideally it would be if we could also access them in old javascript files compiled by sprockets.
I figured out that solution for now. May be helpful for anyone that encounters that problem.
If anyone finds better solution, I would be glad to see it here ;).
For now all our modules/libraries that are needed to be visible in views/other coffee files, I just set as globally available via the window object.
import Foo from '../src/javascript/foo.js
window.Foo = Foo
I know it's ugly anti pattern, but works well as temporary solution until we update our scripts behave more like independent packs.

I see an angular2 'bind' function defined in angular2/angular2.d.ts - did it used to be in 'angular2/di.d.ts?

Many of the samples I have seen for angular2 have the following import statement:
import {bind} from 'angular2/di';
I am working in VS Code (with TypeScript) and it complains about not being able to find the angular2/di module.
However I do see a bind function defined in angular2/angular2.d.ts. If I change the import statement to the following, then the error goes away.
import {bind} from 'angular2/angular2';
Is the question in the title off-base and I am making some erroneous assumption?
If not, why do many samples reference one module to import the bind function from, yet I seem to be able to get it from a different module?
Most likely because you looked at versions from older alphas. Look at the angular2.ts file. Everything is exported from it. Also note that the d.ts is going to contain everything to resolve types in your IDE and at compilation time. What import does is actually importing the .js files.

update module on import to python interpreter

In short
How to force python interpreter to load the most up-to-date code version of my module everytime I make some changes in the module code?
Or at least reload the last modified version by typing
>>> from myModule import *
into console, without necessity to restart whole python console and setup everything again and again anytime I make some changes? This is extremely unpleasant behavior for debugging.
--------- LONGER STORY -----------
I tried to delete the .pyc file, and import it again - but it has no effect. It does't even create .pyc file again - so I expect it completely ignore my "import" command if the module is already loaded.
this also does not help:
>>> mymodule.myfunc() # the old version
>>> del myModule # unload mymodle from python conole / interpeter
... # now I removed .pyc
... # now I make some modifications in mymodule.myfunc() code
>>> mymodule.myfunc() # module is unknonwn, ... OK
>>> import myModule # try to load modified version
>>> mymodule.myfunc() # stil the old version :(((((, How it can remember?
I have tried also Spyder where is this feature called "User Module Deleter (UMD)"
http://pythonhosted.org/spyder/console.html#reloading-modules-the-user-module-deleter-umd
which I thought should do exactly this, but it seem it doesn't (Yes, I checked that it is turned on).
Maybe I'm missing something - can somebody explain me how is it supposed to be used?
Is this somehow affected by the fact that the imported module is not in "Working directory" but in PYTHONPATH ?
(Spyder dev here) I think at the moment you are not able to reload a module directly in the console (but we are considering to change this in the future).
The idea about UMD is that it will reload your modules but only if you run a file from the editor that imports them. It doesn't work if you want to reload them directly in the console.
Let's say you developed a module, then you are probably using it in a different script that (most likely) you'll be writing in our editor and send it to run to our console. UMD is a little bit of magic that reloads it for you when that happens.
Maybe useful for others. In Spyder 3.0.0, the modules can be reloaded by,
Tools -> Update modules names list.
It worked for me.

Resources