Ember.js - Getting App instance in module - ember.js

I have to port a non-cli project to the cli. I have a route that is:
App.thisRoute = App.previouslyDefinedRoute.extend({...})
With the CLI, I've tried this:
// routes/thisRoute.js
import App from 'app';
export default App.previouslyDefinedRoute.extend({...})
That gives me this Ember Inspector Error:
Ember Inspector has errored.
...
Error message: Could not find module app imported from app/routes/thisRoute
I've also tried:
// routes/thisRoute.js
import App from 'routes/previouslyDefinedRoute.js';
export default App.previouslyDefinedRoute.extend({...})
Both dont work.
How do I get my app instance?

There is no app instance in ember-cli, it uses ES6 modules instead, in order to extend a route you will have to import it first.
import PreviousRoute from 'yourProjectName/routes/previouslyDefinedRoute';
Note that the file extension is not needed.
And then you can just export default PreviousRoute.extend({});.
Your files must always export either a default or a named function in order to be used by other files.

Related

How to access config value outside view function in flask

I am trying to access configuration value from my Flask application object outside view function. As I understand from official docs, we can access Flask app instance thats currently running with the help of proxy object called current_app, we can import it with command: from flask import current_app. I need to access config value outside view function. I am using third party API to send sms from my application. I wrote code for this in another separate module and need to access config values in order to initialize custom objects of this third party library.
Here is the code snippets that I wrote inside my module:
from third_party_api import Configuration
from flask import current_app
configuration = Configuration()
configuration.username = current_app.config['third_party_api_username']
configuration.password = current_app.config['third_party_api_password']
Here is the snapshot of RuntimeError I get:
As I understand, we can access current_app config object inside view function without any problem. I dont want to access config value directly from configuration file or class. I read the docs, but it didnt help.
Solved by moving this piece of code to inside the function which is called inside another view function:
configuration = Configuration()
configuration.username = current_app.config['third_party_api_username']
configuration.password = current_app.config['third_party_api_password']

Ember generated service raising error on injection

I am brand new to EmberJS, and am going through a tutorial. I am at the part of the tutorial that starts talking about creating services and injecting them into your controllers (I'm not 100% sure if its a controller, but I'm coming from AngularJS, and it seems pretty similar).
In the terminal in my Ember project, I run the command ember g service authentication. I can see that the service was created in the services directory of my app in the file authentication.js. When I try injecting it into my controller however, I get this issue in the browser when I serve up the app locally,
Error: Assertion Failed: Attempting to inject an unknown injection: 'service:authentication
Could this be because I am using a version of Ember that is newer than in the tutorial, and there is a different way to inject services now? I ask that because this is the syntax of the service in the tutorial
import ember from 'ember'
export default Ember.Service.extend({
records: []
});
and this is the syntax of what I have now, auto-created when the project was built with ember new
import Service from '#ember/service';
export default Service.extend({
records: []
});
org.js "Where service is being injected"
import Route from '#ember/routing/route';
export default Route.extend({
authentication: Ember.inject.service(),
setupController(controller) {
this._super(...arguments);
}
});
Thanks you everyone for all the information about EmberJS.However It turns out all I needed to do was restart my local server -.-
In ember version 2.16, you can leverage javascript module api for importing.
Refer this blog post
Refer this full list
if your ember version is below 2.16, then
import ember from 'ember'
export default Ember.Service.extend({
records: []
});
if ember version is >= 2.16 then the below is the right way to import,
import Service from '#ember/service';
export default Service.extend({
records: []
});
Looks like you are using Ember 2.16 (as others have answered it uses the new components modularization and the tutorial you are following looks like 2.15 or prior, as you have also mentioned in your post).
It makes sense that you should use new modules in your new route too:
import Route from '#ember/routing/route';
import { inject as service } from "#ember/service";
export default Route.extend({
authentication: service()
});
One acclaration: the code you are showing is a route class (there are controllers on the route hierarchy, and also there are components that are not part of the route hierarchy). You will be able to inject your brand-new service in any of the mentioned levels.

Flask: accessing application config in other files (anywhere and not just while serving requests)

I didn't get it clearly from Flask docs. Also, I can see similar stackoverflow questions but I still didn't get my answer, hence asking.
I have a flask application served using gunicorn+gevent. Gunicorn worker process, on start, creates a Flask application. Then it imports some files that setup a few global things, like a udp connection to a statsd server, etc. The setup needs to be done only once i.e. on worker process start and not with every client request. The setup code in the imported files needs access to config variables.
I know that while serving a request I can use the current_app proxy, but not outside a request.
One way can be: put Flask app creation code in a separate file and include it wherever you need access to config.
Ex:
file: mywsgi.py
from flask import Flask
application = Flask(__name__)
application.config.from_pyfile('myconfig.cfg')
file: mygunicornapp.py
from mywsgi import application
import file1
import file2
# import more files
file: file1.py
from mywsgi import application
# use the application config to setup something
file: file2.py
from mywsgi import application
# use the application config to setup something
Is this the preferred way?
Flask doc says I can create application context explicitly.
Can I push application context, just after creating my flask app, and never pop it. So that the application context is always there as long as my process runs and the current_app proxy will be available application wide even when no request being served?
Ex:
from flask import Flask
application = Flask(__name__)
application.config.from_pyfile('myconfig.cfg')
application.app_context().push()
Now I should be able to use the current_app proxy anywhere in my code. Thoughts, please!
== Update ==
The files file1.py, file2.py etc are imported for adding routes to the application. They provide the functions that handle my api requests. So the file mygunicornapp.py looks more like:
file: mygunicornapp.py
from mywsgi import application
from file1 import API1
#application.route("/api1")
def handle_api1():
return API1.handler()
from file2 import API2
#application.route("/api2")
def handle_api2():
return API2.handler()
# many more routes
Now file1 imports many other files and they, in turn, import many more files. Any of these imported files may need access to a config parameter that I have set on the application object. The question is: How do I make the application object available to all these files? Do you suggest that I pass the application object to each file?
Is it possible to just delay adding routes? I mean set routes after current_app context local is available. That means the files will be imported after current_app is available. I tried adding routes to the current_app context local in 'before_first_request' callback. The problem with that is, the very first request returns 404. Subsequent returns give a correct response.
Why don't you make functions in file1 and file2, and pass the argument app into them? Then you can call these functions in your setup code in mywsgi.py, using as an argument the app object you just created.
This should work much better than some of the other things you suggested. The different files importing each other is close to a circular import. Pushing an app context is also something that leaves you likely to end up with difficult to understand bugs.
If you create the object app in one file and import it from that file everywhere, you basically have a global variable (using a namespace). This is going to cause problem when you want to test your app setup code, or create more than one version of your app for another reason. There is also the issue that you won't be able to import any of file1, file2 without creating an app object. While testing these, or possibly re-using some of that code outside of Flask, this will be a pain.
It's much better to create the app object once and pass it around. Having a function which returns the newly created app, which can be imported and called from anywhere, is a common way of organizing a flask app. This file is often called factory.py. It makes it easier to create zero, one or more copies of the app, rather than making it more difficult.

Angular2 cannot find module '../webservices'

In my app.component.ts I import my webservices module as such:
But I'm getting red squiggly lines, even though I think I should be importing it correctly. Here's my app structure:
Here's my web service module:
Must be:
import {Webservice} from './webservices/webservices.services';

Correct way to access current application configuration

I added some configurations to myapp/config/environment:
if (environment === 'development') {
ENV.APP.AuthURL = 'http://localhost:5000/';
}
Now, to access this configuration should I use some method or directly accessing window.Myapp?
You can access it by importing environment.js using the line below:
import config from '../config/environment';
For example, lets say you want to access your configuration in a controller. This is what it would look like:
import Ember from 'ember';
import config from '../config/environment';
export default Ember.Controller.extend({
foo: config.APP.AuthURL
});
If you need to, you can now access it in your controller's template using:
{{foo}}
There are a couple modern ways, as of this writing, when trying to access it from your application:
import ENV from 'your-application-name/config/environment';
your-application-name should be what's in the modulePrefix key of config/environment.js and the name key of package.json
Via Ember.getOwner(this).resolveRegistration('config:environment');
Number one assumes you're using Ember CLI and is detailed in the ember docs under Configuring Your App:
Ember CLI ships with support for managing your application's
environment. Ember CLI will setup a default environment config file at
config/environment. Here, you can define an ENV object for each
environment, which are currently limited to three: development, test,
and production.
The ENV object contains three important keys:
EmberENV can be used to define Ember feature flags (see the Feature Flags guide).
APP can be used to pass flags/options to your application instance.
environment contains the name of the current environment (development,production or test).
You can access these environment variables in your application code by importing from your-application-name/config/environment.
While #rog's answer is correct and will work for all cases where you are trying to access the config from your application there are some edge cases (such as accessing config from an addon) that it will not work for.
I would recommend checking out the ember-get-config addon: https://www.emberobserver.com/addons/ember-get-config
Once you install ember-get-config you can import your config using the following code:
import config from 'ember-get-config';
const { AuthURL } = config;
// now you have access to AuthURL 🎉
This will work in your application and it will also work if you build an addon that will be consumed by your application 👍