Accessing global variable in multiple qml files in blackberry cascades 10 - javascript

I have created a js file for storing constants inside assets/Model/constants.js I had a view inside assets/homeview.qml. This homeview.qml imports
import "Model/constants.js" as Global
and I could access Global.myConstant with no error.
Now I moved the homeview.qml inside a folder. assets/Views/homeview.qml and changed the import location to
import "/Model/constants.js" as Global
but now Global.myConstant shows unknown variable myConstant error. Is there something else I need to do when I move file inside a folder?

Your import should work.
import "/Model/constants.js" as Global
Nevertheless, there is another way to import the file.
import "../Model/constants.js" as Global
This is a relative path to the file. At first you go one directory up and then go into the Model directory containing the constants.js file.

Related

Calling a JS functions from another JS file in EmberJS

I'm currently working in emberJS I need to call a function in the JS file which in the public/myjs.js from
app/components/mycomponent.js. It says could not find module named public/myjs.js
In app/component/mycomponent.js this is the following code I have:
import functions from ../../public/myjs.js
I have also tried this still not working:
const functions = require("myjs");
Thank you :)
files from the public folder can't be imported at the top of files (via normal import X from Y), they are separated from the build system.
the public folder is directly copied into your dist directory.
So a couple options to access myjs.js:
use await import('/myjs.js') -- native dynamic import will load the JS file from the public folder. make note of the leading slash here.
move myjs.js to be within the app folder, and then import X from 'my-app/myjs';

Importing js modules by directory name

I'm upgrading a React application and have found that I need to modify the import statements to get them to work.
For example, in the old version, the following import works without errors:
import { User } from '../System'
Note that System is a directory on my file system that contains User, a js file that ends with export default User.
In my upgraded version of the app, the System directory still exists, but the above import gives me Can't resolve '../System' in 'C:\my app\.
It turns out that to get the import working properly now, I need to change it to the following:
import User from '../System/User';
If I understand correctly, this relates to js module system changes made with ES6.
My question, though, is regarding the specification of a directory in the import statement (System above). Why would it be that I was previously able to name a file directory in the import statement instead of the actual js script/module itself? Is that approach of using a directory in the import statement still possible? And if so, is it ever advisable?
Update: based on AKX's comment, I noticed the System directory does indeed contain an index.js, which apparently is what makes the import from the directory itself possible.
When an import points to a directory, and only a file, Webpack (which most React setups use) follows Node's's conventions and will attempt to import index.js from that directory if it exists. That's the only condition under which importing from a path that points to a directory works - your previous build probably had /System/index.js (which would allow importing with from '../System'). If you rename the file you're importing to anything else - such as to User.js - importing using only the directory path will fail.
And if so, is it ever advisable?
Sure, if you want. It's a style choice but is commonly done.

How to import file from general path in cypress?

I have a file called 'contents' in cypress.
in my test file I want to import this file.
I do the following :
import contents from 'C:/Users/asus/cypress/support/contents';
But I want to make it more general path (not contain my own path).
I tried to import it inside the index file in cypress.
As follow:
import './contents'
But it is not working by this way. Cypress will throw an error that contents is not defined.
Not knowing the data type and assuming that the exact location of the file doesn't matter, I would recommend moving the file to the /fixtures folder and importing it as recommended in the Cypress docs here. So you can use a relative path for the import in your test like:
import contents from '../fixtures/contents';
You can also find an example for importing a JSON file in this answer.
First, you have to export that file
const contents = {
// Your data
}
export { contents };
You can import files like this way
import { contents } from '../support/contents.js'
If ../support/contents.js is not working Please use ../../ to get the root directory of your system like ../../support/contents.js.
Please replace your file extension with .js.

import js package not working inside another import js file in webpack

i am using webpack js bundle for my project.I have published a npm module and which included in my webpack entry file.i can access it inside entry file.But that file i can not access inside another file which included in entry file in webpack.Here is my entry file
import myClass from "sbkkoovery-esign";
console.log(myClass);
import "jquery";
import 'bootstrap';
import "./js/app";
here 'myClass' is working properly.But i tried to access 'myClass' inside '/js/app' file.But i am getting error.Here is mu 'js/app' file
console.log("here");
console.log(myClass);
I was also facing same issue few days back, In this case, rather then to use any node-plugin I have directly given "javascriptfilename.min.js" file path inside tags in "index.html".
Note: This is not the correct solution, this is just temporary.

ES6 imports using module names without path

I would like to be able to import my modules using just name without path.
For example, say I want to use:
import ViewportChecker from 'viewport-checker';
instead of
import ViewportChecker from '../ViewportChecker';
But I want to keep the file where it is and not create a new npm module of it. Would it be possible to define it as module for exmaple in package.json?
You can use webpack's alias feature : https://webpack.js.org/configuration/resolve/#resolve-alias
You can put a default file with naming convention of index.js which contains all the exports.
like:
-App
--home
---index.js // <--it will hold all the exports from otherClass.js and anotherClass.js
---otherClass.js
---anotherClass.js
Now you have to import just folder name and it will automatically search for index.js file to get the particular exported item.
It might look like this:
import Home from './home';

Categories