Customize path on a per-model basis, for the REST adapter - ember.js

According to the URL conventions, it is possible to customize the pluralization, endpoint path and host for the REST adapter. I have a model called VoiceMenu, and the adapter is performing requests to api/voice-menus/, as per the URL-conventions. But they should instead be sent to api/voicemenus/. I do not want to change the name of my model.
How can I configure the REST adapter, for this particular model?

Assuming you have api set already on your adapter, you can set the url on a per model basis like this:
App.VoiceMenu = DS.Model.extend({
url: '/voicemenus'
...
});
Hope it helps.

I got it working with:
App.Adapter.configure('plurals', {
voice_menu : 'voicemenus',
});
I really do not like this, because this has nothing to do with plurals, but I know no other way to configure this.

Related

How to pass through `ember-cli-mirage` request to a specific API and Host

I am trying to use passthrough feature of ember-cli-mirage to allow my app to request to different API and Host.
export default function() {
//window.server = this;
//this.namespace = 'api';
this.passthrough('locales/en/translation.json');
this.get('/api/customers');
this.passthrough();
this.host='https://abcd.site.com';//need something like this, but not working
this.namespace = 'api/Service.svc';
};
I want to point the requests to outside of the environment where current ember server is running.
But the requests which are passing through fixed URL's like /api/authenticate.
It is throwing exceptions as follows.
POST http://localhost:4200/api/authenticate 404 (Not Found)
I want configure the requests to something like this below
https://abcd.site.com/api/Service.svc/authenticate
Is there any option available in ember-cli-mirage/ pretender? Please help.
Passthrough is correct. Just give the full url as parameter, like:
this.passthrough('https://abcd.site.com/api/Service.svc/authenticate');
Take a look at the twitter example here: http://www.ember-cli-mirage.com/docs/v0.2.x/route-handlers/

What RESTAdapter expects on server responses and what requests should server expect?

I'm using Django REST Framework, not Rails (which seems to have several magical gems to make everything work swiftly with Ember) and I've been having some difficulties trying to figure out how Ember expects responses. I'm using Ember CLI, thus I'm also using Ember data.
The documentation states only the typical GET usage, when I'm simply retrieving an object or an array of objects. Documentation states:
The JSON payload should be an object that contains the record inside a root property
And about conventions:
Attribute names in your JSON payload should be the camelCased versions of the attributes in your Ember.js models.
No problem with that.
1. But how should the API respond when there are errors?
Ok, so documentation also states you could use ajaxError to check jqXHR status for an error and then return a populated DS.Error for the record. However, how should I return different kind of errors. For example, let's say the user session is now invalid and because of that the server couldn't delete a record as requested.
2. How will Ember submit requests?
I'm quite new to REST in general. I think Ember simply use the appropriate verb for the action it wants: GET, POST, PUT, DELETE. I think it's quite clear it will send all the model's field to POST a new one, but how about DELETE? Will Ember send all the record or just the ID to delete an object?
Generally you should be able to see the requests Ember makes by just opening your browser dev tools and seeing the network requests.
Ember data likes the api to respond with an errors hash, something like this:
{"errors":{"title":["can't be blank"]}}
Then as long as you define a function to handle the error case:
Ember.Controller.extend({
actions: {
deleteUser: function() {
var user = this.model;
function success() {
// do something cool?
}
function failure() {
user.rollback();
}
user.destroyRecord().then(success, failure);
}
}
});
then user.errors will be automatically populated and you can do an if user.errors in your template.

Loading a relationship from a "RESTful" url

I am trying to figure out how to load a relationship of an object through a sub-resource route using EmberJS.
For example: the url I need to hit on the api is /workspace/:workspace_id/projects, which will return an array of projects related to the workspace. EmberJS is best suited to work with single-level resources, so if I try and do this.store.find('project'), a GET request is fired to /projects.
I have some context for the request, as the url where the request will from from is /workspace/:workspace_id. My gut instinct is to use window.location.pathname in a custom adapter to build the proper request URL.
What would be the proper, "Ember way" of doing this?
What you can do is define the Projects relationship on Workspace as async and then provide the link the right URL to find the workspace's projects. For instance, the response from the endpoint /workspace/1 would be:
{
workspace: {
id: 1,
links: {
projects: "https://example.com/api/workspace/1/projects"
}
}
}
Then if somewhere in the code you would do
this.store.find('workspace', 1).get('projects')
Ember Data will know that the workspace's projects can be found at the link you provided in the JSON response, i.e. "https://example.com/api/workspace/1/projects".
Does this help?

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.

How can i perform a GET request from a separate php file to a uri in my cakephp platform

I'm new with cakephp and I've just finished putting my controllers, models and views in place.I am able to get information from my database through the browser, but i want to achieve this using a seperate php file where i perform a get request. How can i perform a GET request from a separate php file to a uri in my cakephp platform?
I am not sure if this is what you want to accomplish but,
If you want to get the output of your views from another php file (script?), you could use file_get_contents($url), where $url is the uri to your cakephp app.
Hope this helps
If I understand what you mean: Its pretty easy.. add params to the end of your uri: like this yourdomain.com/controller/action/99/foobar
This adds an id and some random param which you can use in your controller with the code below... Just add them as params.
function action($id,$param2){
// use params here
}