oskarrough / ember-wordpress

The bridge between Ember.js and Wordpress
http://ember-wordpress.surge.sh
MIT License
95 stars 26 forks source link

Dynamic namespace RESTAdapter #34

Closed pocockn closed 7 years ago

pocockn commented 7 years ago

I can see in

adapters->wordpress.js

the namespace: wp-json/wp/v2.

I want to be able to fetch the menu items in WordPress using the WP REST API Menus plugin, this exposes an end point like this:

wp-json/wp-api-menus/v2/

Is it possible to make this namespace dynamic when I do a fetch in my route? So in my route where my navigation is I can do:

this.store.findAll('menu');

Where it will hit this end point

wp-json/wp-api-menus/v2/menu

As opposed to if I do the findAll currently it will hit (i assume this end point)

wp-json/wp/v2

And give me the list of menu items.

Is this possible?

Thanks

pocockn commented 7 years ago

I wanted to label this as 'Help Needed' but it wouldn't let me 👎

MartinMalinda commented 7 years ago

You can create an adapter for menu, let it extend the wordpress adapter and override the urlForFindAll method. https://emberjs.com/api/data/classes/DS.RESTAdapter.html#method_urlForFindAll But if you are just fetching one menu and its menu items, I would consider just using simple AJAX.

pocockn commented 7 years ago

To extend the wordpress adapter, could you possibly show me the basic code? I see in the WordPress adapter export default DS.RESTAdapter.extend , so In my adapter what do I extend?

MartinMalinda commented 7 years ago

Sure:)

// /app/adapters/menu.js
import WordpressAadapter from 'ember-wordpress/adapters/wordpress';
export default WordpressAadapter.extend({
urlForFindAll(){
  return 'foo';
}
pocockn commented 7 years ago

Ah thank you

pocockn commented 7 years ago

I've tried this and I got the error findAll is undefined, can you see where I am going wrong?

import WordPressAdapter from 'ember-wordpress/adapters/wordpress';

export default WordPressAdapter.extend({
  urlForFindAll() {
    return '/ember-wp/wp-json/wp-api-menus/v2/';
  }
});

// my route

import Ember from 'ember';
import MenuAdapter from '../adapters/menu-adapter';

export default Ember.Route.extend({

  model() {
    return Ember.RSVP.hash({
      project: this.store.findAll('project'),
      menu: MenuAdapter.store.findAll('menu/2')
    });
  }

});
MartinMalinda commented 7 years ago

There's no need to import adapter and call it like this. Ember-data will look up the menu adapter itself if the modelName is 'menu'

findAll('menu/2') does not seem right as you probably have no model with name menu/2 are you trying to fetch menu with id 2? that would be this.store.findRecord('menu', 2). You'd have to also provide urlForFindRecord then on your adapter.

Maybe it's really easier to just AJAX the menu. It depends on the use-case... :)

pocockn commented 7 years ago

Not sure i'm being stupid but i couldn't get it to work, posted the full example here : http://stackoverflow.com/questions/43287804/custom-adapter-override-find-single-record

oskarrough commented 7 years ago

@pocockn it looks like it's not reading your menu adapter, since the endpoint isn't changed when you request the menu. What is the exact file location of your menu adapter? I believe it should be app/adapters/menu.js.

Thanks for helping @MartinMalinda!

pocockn commented 7 years ago

It's in

app/adapters/menu-adapter.js

do I need to rename to just menu.js?

oskarrough commented 7 years ago

@pocockn yes, believe so.