Hi there! I am recently migrating to Ember Octane and right now I’m trying to figure out how to filter through a model that is passed down to components since there is no two-way binding for component arguments anymore.
Using previous Ember versions I used to do the following:
//app/routes/posts.js
export default Route.extend({
model(){
return this.store.findAll('post');
},
actions: {
filterPosts(postCategory){
this.store.query('post', {category: postCategory}).then((posts) => {
this.controllerFor('posts.index').set('model', posts);
});
}
}
})
//app/templates/posts.hbs
<PostsTable @posts={{model}} filterPosts={{route-action "filterPosts"}}/>
I used the ember-route-action-helper
to get this to work. And from within my component (posts-table
) I just fired the filterPosts
to filter as I needed.
Now with Ember Octane I now (and I tried ) to bind the property that is passed through the component, but it doesn’t work.
So I started to try with queryParams
which I think is a much better way to do this, but I couldn’t reload the model
that is passed down to my component and I have no clue how to do this. Right now I have something like this:
//app/routes/posts.js
import Route from '@ember/routing/route';
import {
inject as service
} from '@ember/service';
export default class PostsRoute extends Route {
@service store;
queryParams = {
category: {
refreshModel: true
}
}
async model(params) {
return this.store.query('posts');
}
}
//app/controllers/posts.js
import {
action
} from "@ember/object";
export default class AppProvasIndexController extends Controller {
queryParams = ['category'];
category = null;
@action
async filter() {
this.model = await this.store.query('posts', {category: 'post-category-to-filter'});
}
}
//app/templates/posts.hbs
<Posts::PostsList @posts={{@model}} />
When I fire the filter
action it brings what I need and it sets the data do this.model
but it won’t reload the model
that is passed to my component. So how can I achieve this behavior using queryParams
? I need to refresh the model that is passed to my component.
I also have tried this:
//app/controllers/posts.js
import {
action
} from "@ember/object";
export default class AppProvasIndexController extends Controller {
queryParams = ['category'];
category = null;
@action
filter() {
this.transitionToRoute('posts.index', { queryParams: {category: 'post-category-to-filter'}});
}
}
But it won’t fire the transitionToRoute
and I don’t have any error logs too.
Is that the right approach or would you suggest a better way to do this?
The use case is: I have a non-filtered model that is fetched from the store and passed down to a component. I need to filter this model with some params that the user will choose and update the data that is passed down to the component.
I need to refresh the model because I also have paginated data so It would be right to filter just the data that I already have fetched from the store.
I am a little bit lost here and would appreciate your help and suggestions. Thank you so much!