Ember model host adapter not overiding mirage - ember.js

I'm using Ember-cli-mirage to mock data. I want to slowly integrate parts of the production api which is located on my local machine at http://localhost:8000. Ember docs tell me that I should be able to set an adapter so I can have a different host for each model.
I have a customer model, and have setup ember-cli-mirage which is successfully serving data. The customer model is the first model I want to split out to localhost:8000.
I've setup adapters/customer.js with the following:
import DS from 'ember-data';
export default DS.RESTAdapter.extend( {
host: 'http://localhost:8000',
namespace: 'api/v1'
});
But when I make the call I'm getting an error:
Mirage: Error: Your Ember app tried to GET 'http://localhost:8000/api/v1/customers',
but there was no route defined to handle this request.
Define a route that matches this path in your
mirage/config.js file. Did you forget to add your namespace?
And my header inspector shows that customers is making the request to the mirage server:
Request URL:http://localhost:6543/customers
Request Method:GET
Status Code:304 Not Modified
Remote Address:[::1]:6543
I suspect it's something to do with my config/environment.js setup so I'm looking at a variation of https://github.com/samselikoff/ember-cli-mirage/issues/497#issuecomment-183458721 as a potential workaround. But I can't see why mirage won't accept the adapter overide.

Should have read back through the mirage docs for this one. There's a passthrough function that allows mirage to pass certain requests through to Ember bypassing mirage:
// mirage/config.js
import Mirage from 'ember-cli-mirage';
export default function() {
this.urlPrefix = 'http://localhost:8000';
this.namespace = '/api/v1';
// Requests for customers
this.get('/customers');
this.get('/customers/:id');
this.post('/customers');
this.del('/customers/:id');
this.patch('/customers/:id');
// Passthrough to Django API
this.passthrough('/customers');
}
To make this work in my application adapter I added:
// app/adapters/application.js
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
host: 'http://localhost:8000',
namespace: 'api/v1'
});
If this helps you in any way feel free to give this answer an upvote :)

Related

Ember: Local config for development

I want to share my ember-project between all members in my team.
Since every development environment is different (OS, VM for server, etc.) I thought it would be best practice to hold an local-environmen-file in each repository clone which will be ignored by git and just holds the specific config of each team-member.
For Example in my app the application adapter holds the "host:" entry which is a computed-property which uses the local-environment-file and reads the IP of the server (where to connect to).
My current approach looks like that:
// ./config/localenv.js
module.exports = function() {
var LOCALENV = {
host: "http://192.168.1.44:3000"
};
return LOCALENV;
};
And Im importing it in my adapter like that:
// ./app/pods/application/adapter.js
import DS from 'ember-data';
import LOCALENV from 'your-application-name/config/localenv';
export default DS.RESTAdapter.extend({
host: Ember.computed(function() {
return LOCALENV.host;
})
});
Unfortunately ember returns error logs in the browser console (the referenced/imported module was not found...). What am Im doing wrong?

Ember-cli-mirage not showing any data with get

I am learning Ember and I am getting stuck on making the mock api with ember-cli-mirage. I modified the config file as specified in the ember tutorial as well as on the ember-cli-mirage site, but everytime I hit the endpoint I get nothing. Here is my current config file
export default function() {
this.get('/api/users', function() {
return {
users: [
{id: 1, name: 'Zelda'},
{id: 2, name: 'Link'},
{id: 3, name: 'Epona'},
]
}
});
}
Like I said, when I go to /api/users it is just a blank page. Am I missing something here?
Thanks!
First thing first, install Ember inspector extension (for Chrome or Firefox) and look in the browser console to see if Mirage is giving you some errors. If nothing is written in there, you are not hitting the endpoint with your ember application. Basically, Mirage proxies all the request from your ember application.
So you need to generate a user model
ember g model user
and put in there the name attribute.
Create a route and in the model hook write
return this.get('store').findAll('user');
(look at the quick start tutorial if something is not clear)
So now, leveraging Ember Data, your app will request all users hitting on /users.
Now let's start with mirage, generate a mirage model
ember g mirage-model user
and follow the mirage quickstart, just adapt it to your needs :)
Start your application with ember s and you should see the request to /users.
If you want to put your api on the same domain, but with the /api prefix, then i suggest you to read about endpoint path customization
In app/mirage/config.js you can set up mock endpoints for your users:
export default function() {
this.get('/users');
this.post('/users');
this.put('/users/:id');
this.del('/users/:id');
}
You can set up your mock data by configuring fixtures in app/mirage/fixtures/users.js:
export default [
{id: 1, name: 'Zelda'},
{id: 2, name: 'Link'},
{id: 3, name: 'Epona'},
];
Mirage isn't an actual server, so you won't be able to hit the API from your browser directly. It's a mock server that lives in JavaScript memory, and is instantiated when your Ember app boots up.
To test out your mocks, have your Ember app make an API request, e.g.
// routes/application.js
export default Ember.Route.extend({
model() {
return Ember.$.getJSON('/api/users');
}
});
If everything's hooked up correctly, you should now see Mirage handling this request and logging the response data in your console.

Get Ember Data to accept different key pair

My API works almost the way Ember wants it to, with one exception. All my JSON is under the namespace "data". Ember wants it to be under the namespace of the model, like "users". How can I tell ember to just use the "data" key?
What does your app/application/adapter.js file look like? Take a look at the Ember docs for customizing your endpoints. My guess is that it should look something like this for you:
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
namespace: 'data'
});
You can intercept the initial response payload in adapter's handleResponse hook: http://emberjs.com/api/data/classes/DS.RESTAdapter.html#method_handleResponse
There you can overwrite the root of the payload with the one that Ember Data expects.

Ember CLI - Simple Auth - Custom Authenticator not automatically registering

I'm attempting to setup a custom authenticator with ember simple auth. I'm using Ember CLI and according to the Simple Auth ReadMe on GitHub it states. Note that when you're not using Ember CLI the authenticator will not be registered with the container automatically and you need to do that in an initializer.
It does not state where you need to put your authenticator (or authorizer for that matter) in your directory structure in order for it to be registered by Ember CLI automatically. After creating my file in app/authenticators/custom.js (as shown in the examples of the read me) I expected it to be registered with the container. Looking in Ember Inspector it's no where to be found.
Does anyone have any insight into this? Where are these files meant to be placed?
Please ask if any additional information is needed.
Ember: 1.7.0
Ember Data: 1.0.0-beta.10
Ember Simple Auth: 0.7.1
The latest version of Ember CLI should actually auto-register the authenticator - make sure you're using that (you probably aren't as you're still at Ember 1.7.0). That should solve it.
Make sure you have your initializer in /app/initializers/. Initializers in this directory are automatically set up by ember-cli.
// app/initializers/authentication.js
import CustomAuthenticator from '../authenticators/custom';
export default {
name: 'authentication',
before: 'simple-auth',
initialize: function(container, application) {
container.register('authenticator:custom', CustomAuthenticator);
}
};
I am getting the same issue and I have Ember 1.8.1
Error is:
Uncaught ReferenceError: CustomAuthenticator is not defined
in file app/authenticators/custom.js
I needed to add an initializer and change the code found in the docs to this below, and it works
import Base from 'simple-auth/authenticators/base';
var CustomAuthenticator = Base.extend({
restore: function(data) {
},
authenticate: function(options) {
},
invalidate: function(data) {
}
});
export default CustomAuthenticator;

CLOSED Ember-cli : Error while processing route:

I've got a problem to create a route to my model with ember.js. I've got the following messages:
GET http://localhost:4200/contacts 404 (Not Found)
Error while processing route: contacts'
This is my code :
// app/models/contacts.js
import DS from 'ember-data';
export default DS.Model.extend({
lastname: DS.attr('string'),
firstname: DS.attr('string')
});
// app/routes/contacts.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return this.store.find('contact');
}
});
// app/templates/application.hbs
<h2 id='title'>Welcome to Ember.js</h2>
{{link-to 'Mes contacts' 'contacts'}}
{{outlet}}
// app/templates/contacts.hbs
<h3>Liste des contacts</h3>
{{outlet}}
// app/router.js
import Ember from 'ember';
import config from './config/environment';
var Router = Ember.Router.extend({
location: config.locationType
});
Router.map(function() {
this.resource('contacts');
});
export default Router;
These messages appear when I click in the link "Mes contacts".
I use Ember v1.8.1.
Someone could help me ?
Thanks by advance.
Install the Ember Inspector if you haven't done already. Then you can see the promises that are failing.
Also the normal browser Network inspector will show you the request payloads so you can see what is being sent and received.
Have you verified that the request actually goes to the server?
Have you verified that the server is able to precess the request?
(as it appears the server does not know the endpoint provided).
Have you verified that the server is sending a response with the expected payload?
Does the browser network inspector response payload show the expected result?
I had the same issue. Sometimes Ember is not showing the details of an error.
I ended putting a breakpoint in my vendor.js on the following line:
TRY_CATCH_ERROR.error = e;
I know you can also enable more details if needed but I haven't tried yet.
Bon courage! :)
I had issues with ember-cli when i've upgraded to the latest ember.js v1.8.1 with bower, so i downgraded ember to the version which is bundled with ember cli and it worked again.
I am new to ember.js, i find it hard to upgrade to latest version without breaking the app because of incompatibility with the generator and addons like ember-data.
Try using version bundled with ember-cli to see if it is an upgrade issue.
OK, I start a new project and I don't have this problem.
But I don't know what I did wrong..
I close this topic.