-
-
Save jameshahn2/072e195fd54b683abd93e8853a70b3ca to your computer and use it in GitHub Desktop.
Filtering Async hasMany Relationships
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DS from 'ember-data'; | |
export default DS.RESTAdapter.extend({ | |
namespace: 'api', | |
//coalesceFindRequests: true | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
queryParams: ['petName'], | |
petName: null, | |
filteredOwners: Ember.computed('petName', 'model.@each.pets.[]', function() { | |
let petName = this.get('petName'); | |
let ownersArray = this.get('model').toArray(); | |
let filterPromise = Ember.RSVP.filter(ownersArray, owner => { | |
return owner.get('pets').then( pets => { | |
return pets.isAny('name', petName); | |
}); | |
}); | |
return DS.PromiseArray.create({ | |
promise: filterPromise | |
}); | |
}) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function() { | |
const owners = { | |
owners: [ | |
{ id: 1, name: 'John', pets: [1, 2] }, | |
{ id: 2, name: 'Sarah', pets: [3, 1] }, | |
{ id: 3, name: 'Chris', pets: [4, 3] } | |
] | |
} | |
const pet1 = { id: 1, name: 'Tom', owners: [1, 2] } | |
const pet2 = { id: 2, name: 'Sigmund', owners: [1] } | |
const pet3 = { id: 3, name: 'Rocky', owners: [2, 3] } | |
const pet4 = { id: 4, name: 'Buster', owners: [3] } | |
const pets = { | |
pets: [ pet1, pet2, pet3, pet4 ] | |
} | |
getJSON('/api/owners', owners); | |
getJSON('/api/pets', pets); | |
getJSON('/api/pets/1', { pet: pet1 }); | |
getJSON('/api/pets/2', { pet: pet2 }); | |
getJSON('/api/pets/3', { pet: pet3 }); | |
getJSON('/api/pets/4', { pet: pet4 }); | |
} | |
function getJSON(url, jsonResponse) { | |
$.mockjax({ url: url, responseText: jsonResponse }); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DS from 'ember-data'; | |
export default DS.Model.extend({ | |
name: DS.attr('string'), | |
pets: DS.hasMany('pet') | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DS from 'ember-data'; | |
export default DS.Model.extend({ | |
name: DS.attr('string'), | |
owners: DS.hasMany('owner') | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import getMocks from '../mocks'; | |
export default Ember.Route.extend({ | |
init() { | |
this._super(...arguments); | |
getMocks(); | |
}, | |
model() { | |
return this.store.findAll('owner'); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.15.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js", | |
"ember": "3.4.3", | |
"ember-template-compiler": "3.4.3", | |
"ember-testing": "3.4.3" | |
}, | |
"addons": { | |
"ember-data": "3.4.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment