-
-
Save martin-hoger/0d7fa052e2a64fc071f3f495169c5b62 to your computer and use it in GitHub Desktop.
Filtering Async hasMany Relationships
This file contains hidden or 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 hidden or 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.Component.extend({ | |
store: Ember.inject.service(), | |
click: function() { | |
//alert(this.get("store").findAll('owner')); | |
let pet = this.get('store').peekRecord('pet', 1); | |
this.get("store").createRecord('owner', { | |
name: 'Rails is Omakase', | |
pets: [pet] | |
}); | |
//alert("adfa"); | |
} | |
}); |
This file contains hidden or 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', '[email protected].[]', 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.4.8", | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.1.0", | |
"ember-data": "2.1.0", | |
"ember-template-compiler": "2.1.0", | |
"jquery-mockjax": "https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.6.2/jquery.mockjax.js" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment