I have a model named List that represents a navigation tree. I should be able to navigate into a child list, and then hit the back button to go back up to the parent list.
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
children: DS.hasMany('list', { inverse: 'parent' }),
parent: DS.belongsTo('list', { inverse: 'children' })
});
I determine the "root" level list in my routes file by looking for the list that has no parents.
import Ember from 'ember';
export default Ember.Route.extend({
model(){
return this.store.findAll('list').then(function(lists){
return lists.filter(function(list){
if(Ember.isEmpty(list.get('parent').get('content'))) {
return true;
}
else {
return false;
}
}).get('firstObject');
});
}
});
There's a number of things I feel like are wrong about my implementation, even though it works on the initial page load.
Ember.isEmpty(list.get('parent').get('content'))completely seems like the wrong way to check for the existence of the parent relationship.- I feel like I may not be filtering the entire list collection correctly. I'm intentionally calling the index API request so that all lists are loaded, but when I navigate to a child list and hit the back button, the XHR request is made a second time!
- Building on top of that, the filtering only works for the initial page load. If I go down into a list and then hit the back button, the child list persists on the page (no errors or anything!).
What is the right way to check existence of the parent relationship?
Also, what's the right way to filter the entire list to find one record?
Is my ignorance what's causing issues with the back button?
Aucun commentaire:
Enregistrer un commentaire