Created
November 9, 2015 17:29
-
-
Save anonymous/71078b771429753043ff to your computer and use it in GitHub Desktop.
Filtering hasMany Async Relationships in Ember // source http://jsbin.com/busoje
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Filtering hasMany Async Relationships in Ember</title> | |
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.1/normalize.css"> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | |
<script src="http://builds.emberjs.com/tags/v2.1.0/ember-template-compiler.js"></script> | |
<script src="http://builds.emberjs.com/tags/v2.1.0/ember.js"></script> | |
<script type="text/javascript" src="http://builds.emberjs.com/tags/v2.1.0/ember-data.js"></script> | |
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.5.3/jquery.mockjax.js"></script> | |
<style id="jsbin-css"> | |
/* Put your CSS here */ | |
html, body { | |
margin: 20px; | |
} | |
</style> | |
</head> | |
<body> | |
<script type="text/x-handlebars"> | |
<h2>Filtering hasMany Async Relationships in Ember</h2> | |
<small>Ember version: 2.1</small> | |
<p>This simple example demonstrates how you can filter results depending on a hasMany relationship.</p> | |
<p>By clicking on the names of the pets you can filter the owners.</p> | |
{{outlet}} | |
</script> | |
<script type="text/x-handlebars" data-template-name="index"> | |
<h3>All Owners</h3> | |
<ul> | |
{{#each owners as |owner|}} | |
<li> | |
{{owner.name}} | |
<ul> | |
{{#each owner.pets as |pet|}} | |
<li>{{#link-to 'index' (query-params petName=pet.name)}}{{pet.name}}{{/link-to}}</li> | |
{{/each}} | |
</ul> | |
</li> | |
{{/each}} | |
</ul> | |
<h3>Owners with a pet called {{input value=petName}}</h3> | |
{{#if filteredOwners.isPending}} | |
<p>Loading owners...</p> | |
{{else}} | |
<ul> | |
{{#each filteredOwners as |owner|}} | |
<li> | |
{{owner.name}} | |
</li> | |
{{else}} | |
<li>No owners found...</li> | |
{{/each}} | |
</ul> | |
{{/if}} | |
</script> | |
<script id="jsbin-javascript"> | |
var App = Ember.Application.create(); | |
App.ApplicationAdapter = DS.RESTAdapter.extend({}); | |
App.IndexRoute = Ember.Route.extend({ | |
model: function() { | |
return this.store.findAll('owner'); | |
} | |
}); | |
App.IndexController = Ember.Controller.extend({ | |
queryParams: ['petName'], | |
petName: 'Tom', | |
owners: Ember.computed.alias('model'), | |
filteredOwners: Ember.computed('petName', '[email protected].{id, name}', function() { | |
var petName = this.get('petName'); | |
return DS.PromiseArray.create({ | |
promise: Ember.RSVP.filter(this.get('model').toArray(), owner => { | |
return owner.get('pets').then(pets => { | |
return pets.isAny('name', petName); | |
}); | |
}) | |
}); | |
}) | |
}); | |
App.Owner = DS.Model.extend({ | |
name: DS.attr('string'), | |
pets: DS.hasMany('pet', {async: true}), | |
}); | |
App.Pet = DS.Model.extend({ | |
name: DS.attr('string'), | |
owners: DS.hasMany('owner', {async: true}) | |
}); | |
$.mockjax({ | |
url: '/owners', | |
responseText: { | |
owners: [ | |
{ | |
id: 1, | |
name: 'John', | |
pets: [1, 2] | |
}, | |
{ | |
id: 2, | |
name: 'Sarah', | |
pets: [3, 1] | |
}, | |
{ | |
id: 3, | |
name: 'Chris', | |
pets: [4, 3] | |
}, | |
] | |
} | |
}); | |
$.mockjax({ | |
url: '/pets/1', | |
responseText: { | |
pet: { | |
id: 1, | |
name: 'Tom', | |
owners: [1, 2] | |
} | |
} | |
}); | |
$.mockjax({ | |
url: '/pets/2', | |
responseText: { | |
pet: { | |
id: 2, | |
name: 'Sigmund', | |
owners: [1] | |
} | |
} | |
}); | |
$.mockjax({ | |
url: '/pets/3', | |
responseText: { | |
pet: { | |
id: 3, | |
name: 'Rocky', | |
owners: [2, 3] | |
} | |
} | |
}); | |
$.mockjax({ | |
url: '/pets/4', | |
responseText: { | |
pet: { | |
id: 4, | |
name: 'Buster', | |
owners: [3] | |
} | |
} | |
}); | |
</script> | |
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Filtering hasMany Async Relationships in Ember</title> | |
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.1/normalize.css"> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"><\/script> | |
<script src="http://builds.emberjs.com/tags/v2.1.0/ember-template-compiler.js"><\/script> | |
<script src="http://builds.emberjs.com/tags/v2.1.0/ember.js"><\/script> | |
<script type="text/javascript" src="http://builds.emberjs.com/tags/v2.1.0/ember-data.js"><\/script> | |
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.5.3/jquery.mockjax.js"><\/script> | |
</head> | |
<body> | |
<script type="text/x-handlebars"> | |
<h2>Filtering hasMany Async Relationships in Ember</h2> | |
<small>Ember version: 2.1</small> | |
<p>This simple example demonstrates how you can filter results depending on a hasMany relationship.</p> | |
<p>By clicking on the names of the pets you can filter the owners.</p> | |
{{outlet}} | |
<\/script> | |
<script type="text/x-handlebars" data-template-name="index"> | |
<h3>All Owners</h3> | |
<ul> | |
{{#each owners as |owner|}} | |
<li> | |
{{owner.name}} | |
<ul> | |
{{#each owner.pets as |pet|}} | |
<li>{{#link-to 'index' (query-params petName=pet.name)}}{{pet.name}}{{/link-to}}</li> | |
{{/each}} | |
</ul> | |
</li> | |
{{/each}} | |
</ul> | |
<h3>Owners with a pet called {{input value=petName}}</h3> | |
{{#if filteredOwners.isPending}} | |
<p>Loading owners...</p> | |
{{else}} | |
<ul> | |
{{#each filteredOwners as |owner|}} | |
<li> | |
{{owner.name}} | |
</li> | |
{{else}} | |
<li>No owners found...</li> | |
{{/each}} | |
</ul> | |
{{/if}} | |
<\/script> | |
</body> | |
</html> | |
</script> | |
<script id="jsbin-source-css" type="text/css">/* Put your CSS here */ | |
html, body { | |
margin: 20px; | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var App = Ember.Application.create(); | |
App.ApplicationAdapter = DS.RESTAdapter.extend({}); | |
App.IndexRoute = Ember.Route.extend({ | |
model: function() { | |
return this.store.findAll('owner'); | |
} | |
}); | |
App.IndexController = Ember.Controller.extend({ | |
queryParams: ['petName'], | |
petName: 'Tom', | |
owners: Ember.computed.alias('model'), | |
filteredOwners: Ember.computed('petName', '[email protected].{id, name}', function() { | |
var petName = this.get('petName'); | |
return DS.PromiseArray.create({ | |
promise: Ember.RSVP.filter(this.get('model').toArray(), owner => { | |
return owner.get('pets').then(pets => { | |
return pets.isAny('name', petName); | |
}); | |
}) | |
}); | |
}) | |
}); | |
App.Owner = DS.Model.extend({ | |
name: DS.attr('string'), | |
pets: DS.hasMany('pet', {async: true}), | |
}); | |
App.Pet = DS.Model.extend({ | |
name: DS.attr('string'), | |
owners: DS.hasMany('owner', {async: true}) | |
}); | |
$.mockjax({ | |
url: '/owners', | |
responseText: { | |
owners: [ | |
{ | |
id: 1, | |
name: 'John', | |
pets: [1, 2] | |
}, | |
{ | |
id: 2, | |
name: 'Sarah', | |
pets: [3, 1] | |
}, | |
{ | |
id: 3, | |
name: 'Chris', | |
pets: [4, 3] | |
}, | |
] | |
} | |
}); | |
$.mockjax({ | |
url: '/pets/1', | |
responseText: { | |
pet: { | |
id: 1, | |
name: 'Tom', | |
owners: [1, 2] | |
} | |
} | |
}); | |
$.mockjax({ | |
url: '/pets/2', | |
responseText: { | |
pet: { | |
id: 2, | |
name: 'Sigmund', | |
owners: [1] | |
} | |
} | |
}); | |
$.mockjax({ | |
url: '/pets/3', | |
responseText: { | |
pet: { | |
id: 3, | |
name: 'Rocky', | |
owners: [2, 3] | |
} | |
} | |
}); | |
$.mockjax({ | |
url: '/pets/4', | |
responseText: { | |
pet: { | |
id: 4, | |
name: 'Buster', | |
owners: [3] | |
} | |
} | |
}); | |
</script></body> | |
</html> |
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
/* Put your CSS here */ | |
html, body { | |
margin: 20px; | |
} |
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
var App = Ember.Application.create(); | |
App.ApplicationAdapter = DS.RESTAdapter.extend({}); | |
App.IndexRoute = Ember.Route.extend({ | |
model: function() { | |
return this.store.findAll('owner'); | |
} | |
}); | |
App.IndexController = Ember.Controller.extend({ | |
queryParams: ['petName'], | |
petName: 'Tom', | |
owners: Ember.computed.alias('model'), | |
filteredOwners: Ember.computed('petName', '[email protected].{id, name}', function() { | |
var petName = this.get('petName'); | |
return DS.PromiseArray.create({ | |
promise: Ember.RSVP.filter(this.get('model').toArray(), owner => { | |
return owner.get('pets').then(pets => { | |
return pets.isAny('name', petName); | |
}); | |
}) | |
}); | |
}) | |
}); | |
App.Owner = DS.Model.extend({ | |
name: DS.attr('string'), | |
pets: DS.hasMany('pet', {async: true}), | |
}); | |
App.Pet = DS.Model.extend({ | |
name: DS.attr('string'), | |
owners: DS.hasMany('owner', {async: true}) | |
}); | |
$.mockjax({ | |
url: '/owners', | |
responseText: { | |
owners: [ | |
{ | |
id: 1, | |
name: 'John', | |
pets: [1, 2] | |
}, | |
{ | |
id: 2, | |
name: 'Sarah', | |
pets: [3, 1] | |
}, | |
{ | |
id: 3, | |
name: 'Chris', | |
pets: [4, 3] | |
}, | |
] | |
} | |
}); | |
$.mockjax({ | |
url: '/pets/1', | |
responseText: { | |
pet: { | |
id: 1, | |
name: 'Tom', | |
owners: [1, 2] | |
} | |
} | |
}); | |
$.mockjax({ | |
url: '/pets/2', | |
responseText: { | |
pet: { | |
id: 2, | |
name: 'Sigmund', | |
owners: [1] | |
} | |
} | |
}); | |
$.mockjax({ | |
url: '/pets/3', | |
responseText: { | |
pet: { | |
id: 3, | |
name: 'Rocky', | |
owners: [2, 3] | |
} | |
} | |
}); | |
$.mockjax({ | |
url: '/pets/4', | |
responseText: { | |
pet: { | |
id: 4, | |
name: 'Buster', | |
owners: [3] | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment