oskarrough / ember-wordpress

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

Wrong/cached model loading despite changing route #42

Closed gregorymark closed 6 years ago

gregorymark commented 6 years ago

Awesome work on this addon, it's made my life much easier!

I was having a real issue working out why Ember was caching my first loaded model, despite changing routes in my build. It seems that because I was not defining models in my app (as in the demo) but relying on the addon defined models, the models were all being initially cached as having the same model type. Therefore subsequent calls to the API were building the same URL from the same model name, though with the correct parameters. The result was that it was returning e.g. pages when I wanted posts if the first route loaded was a page and the second to posts.

Adding models in my app as simply as

import Post from 'ember-wordpress/models/post';

export default Post.extend({
});

solved the problem, though using

export { default } from 'ember-wordpress/models/post';

everywhere didn't.

I'm pretty new to Ember so it's possible I'm making a rookie error here or elsewhere (though I built a minimal use-case as close to the demo as I could get and was still having the same issue), but I noticed that the demo build uses a pretty old version of Ember - is that behaviour that's changed? Is it worth updating the demo build? It certainly took me a while to track down!

For the record this is how I'm loading my /about route

import Route from '@ember/routing/route';

export default Route.extend({
    model() {
        return this.store.query('page', {slug: 'about'}).then(models => models.get('firstObject'));
    }
});

Anyway, working now, mostly to save time for anyone else who's having this issue, unless it's worth updating the demo environment if I'm correct.

oskarrough commented 6 years ago

Almost doesn't sound like it made it easier hehe. But thank you!

You might have found something here. The addon gives you a post as well as a page model. Behind the scenes these two both extend from the same addon/models/post.js.

Can you reproduce it in the demo app?

gregorymark commented 6 years ago

Hehe, I'd definitely still be working things out if I didn't have the addon!

So when you say reproduce it in the demo app I've tried installing this as my app, but I can't seem to get past a load of errors (not finding files and so on). Is that what you mean?

oskarrough commented 6 years ago

Almost. If you git clone this repository and run ember serve it'll serve the "dummy app" (same as you linked).

I thought if you can reproduce the error there as well it would be easier to track it down :)

gregorymark commented 6 years ago

Right, yes. Easier than I thought :)

It's having the same problem there. Categories load because they're using a different model, but requests using the same model use the first model name requested.

If I change the models defined in app/models as follows

import Post from 'ember-wordpress/models/post';

export default Post.extend({
});

then it works and requests the correct URL.

oskarrough commented 6 years ago

@gregorymark mind verifying that it works here? https://github.com/oskarrough/ember-wordpress/pull/44. Then I can merge and release a new version.

gregorymark commented 6 years ago

@oskarrough yep that's working.