Ember data and RestAdapter - ember.js

I'm building an ember application with a RESTAdapter to access my data in an api. I'm using the latest version of ember-data from https://github.com/emberjs/data/downloads.
This is how I'm declaring my RESTAdatpter-
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://example.com'
});
but I dont think it's declared correctly as the ember inspector in the browser says that it cannot detect an adapter. Where am I going wrong?

I had the same issue and was able to get it to work using the following:
DS.RESTAdapter.reopen({
host: 'http://example.com'
})
I'm not sure if the guides need to be updated or if there's a different way to accomplish this.

It worked when I changed the ember data version to 1.0.0-beta.

Related

Ember Simple Auth: Session - no authorize method?

I am reading documentation of the Ember Simple Auth. I want to send authorized ajax request (outside of the Ember store).
I've found out there is a method for that. However when I try it to call inside my component, I get error "TypeError: this.get(...).authorize is not a function". What am I doing wrong?
Here's my code:
import Ember from "ember";
export default Ember.Component.extend({
session: Ember.inject.service('session'),
actions: {
del: function() {
var self = this;
console.log(this.get('session'));
this.get('session').authorize('authorizer:oauth2-bearer', function(header, content) {
...
});
}
}
});
One rule you should apply when you upgrade ember plugin - make sure the older NPM version is uninstalled properly - or you will have many, many problems like I had.
Uninstalling the older version completly solved my problem.

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.

Setting host on DS.RESTAdapter removing ember-simple-auth headers

I'm using the ember-cli-simple-auth with ember-cli and everything it working great, until I try and set a new host on DS.RESTAdapter application wide.
As soon as I set
// adapters/application.js
exports default DS.RESTAdapter.extend({
host: 'https://api.example.com'
});
or even using reopen() it clears all the headers set by ember-simple-auth.
Am I not setting this up right in ember-cli?
If you're using a different domain for the REST API you need to configure that for OAuth 2.0 authenticator (assuming you're using that) and also make sure you have CORS enabled on the server side. You can find a tutorial here: http://log.simplabs.com/post/90339547725/using-ember-simple-auth-with-ember-cli.

Ember data post to Web API

How can I get my web api to understand the format returned by ember data?
ember data is sending like this:
{client: {firstName:"",lastName:""}}
As far as I understand web api is looking for
{firstName:"",lastName:""}
After so much googling I finally get the right terms and figured out how to do it. Simple serilizer update on the ember data side:
App.ApplicationSerializer = DS.RESTSerializer.extend({
serializeIntoHash: function (hash, type, record, options) {
Ember.merge(hash, this.serialize(record, options));
}
});

Ember data 1.0.0 Beta: RESTAdapter endpoint customization no longer works

I am converting a project for use with Ember data 1.0.0 Beta 1 (just released). I have a REST adapter listening on a specific endpoint and thus need to customize the endpoint.
This is how it worked in Ember data 0.13:
App.Adapter = DS.RESTAdapter.extend({})
DS.RESTAdapter.reopen({
url: 'https://api.example.com'
});
In Ember data 0.13, the URL became: https://api.example.com/authors
In Ember data 1.0.0, the url becomes: http://192.168.0.108:51939/authors
with /192.168.0.108:51939 the url on which the webapp is running.
It thus looks like the url setting on .reopen of a RESTAdapter no longer works ?
I have the same problem with other customizations of the URL (such as namespace) ...
Hope somebody can help.
Marc
Looks like this was updated soon after #cyclomarc's answer (check the PR https://github.com/emberjs/data/pull/1145). In ember data 'url' is now 'host'. 'namespace' stills works.
DS.RESTAdapter.reopen({
host: 'http://google.com',
namespace: 'api'
});
Sends requests to http://google.com/api/*
Ember v1.0.0-7
Ember Data v1.0.0-beta.1-17
EDIT: This is now documented in TRANSITION.md:
https://github.com/emberjs/data/blob/master/TRANSITION.md#host-and-namespace-configuration
Ember-Data 1.0 beta is a complete rewrite of the API, see the transition guide, which details the changes made
The transition guide mentions that the Adapter API has changed, and adapters will have to be rebuilt. This is likely a breaking change, and the documentation is forthcoming on the endpoint customization
Seems to be a regression. A PR is registered by Paul Chavard.
See https://github.com/emberjs/data/pull/1145
In the meantime, overriding the buildUrl is a solution (see answer from #intuitivepixel)
https://github.com/emberjs/data/blob/master/TRANSITION.md
http://emberjs.com/guides/models/connecting-to-an-http-server/
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://api.example.com',
namespace: 'admin'
})
Refer to the links above.
Note that with the current ember-data beta you must call your custom adapter "App.ApplicationAdapter".
Doesn't work if you try "App.Adapter".
Hope that helps!
It seems the RESTAdapter in beta1 has quite a few regressions. I'm looking at it now and so far I see missing:
namespace/url configuration
camelCase to lower_with_underscore attribute mapping
query params on GET url
Non of the above is mentioned in the transition guide (unless I completely missed it).
Having looked into the transition guide, still no mention about that url and namespace are removed from the RESTAdapter, further reading in the source code inline comments still refer it can be used as mentioned in the question. But as #cyclomarc mentioned in his comment (referring to what #tchak13 said that one should now use buildURL), so this is how you could do it overriding the buildURL function:
App.Adapter = DS.RESTAdapter.extend({
buildURL: function(type, id) {
var url = "/" + Ember.String.pluralize(type.typeKey);
if (id) { url += "/" + id; }
return 'https://api.example.com' + url;
}
});
Hope it helps.