Skip to content

Instantly share code, notes, and snippets.

@benoror
Forked from Gaurav0/application.route.js
Last active April 12, 2016 22:19
Show Gist options
  • Save benoror/f5e1039387317769b13394120ec19222 to your computer and use it in GitHub Desktop.
Save benoror/f5e1039387317769b13394120ec19222 to your computer and use it in GitHub Desktop.
Country/States Cascaded
import DS from 'ember-data';
const ActiveModelAdapter = DS.ActiveModelAdapter;
const ApplicationAdapter = ActiveModelAdapter.extend({
shouldReloadAll: function(store, snapshotRecordArray) {
return true;
},
shouldBackgroundReloadRecord: function(store, snapshot) {
return false;
}
});
export default ApplicationAdapter;
import ApplicationAdapter from './application';
const CountryAdapter = ApplicationAdapter.extend({
findAll() {
return $.getJSON('/api/countries');
}
});
export default CountryAdapter;
import ApplicationAdapter from './application';
const StateAdapter = ApplicationAdapter.extend({
});
export default StateAdapter;
import Ember from 'ember';
const {
get,
set
} = Ember;
export default Ember.Route.extend({
init() {
this._super(...arguments);
$.mockjax({
url: '/api/countries',
responseText: {
countries: [
{
id: 140,
name: "USA"
},
{
id: 141,
name: "France"
},
{
id: 142,
name: "Mexico"
}
]
}
}, {
url: '/api/countries/140/states',
responseText: {
states: [
{
id: 1,
name: "Texas",
country_id: 140
},
{
id: 2,
name: "Nevada",
country_id: 140
},
{
id: 3,
name: "California",
country_id: 140
}
]
}
}, {
url: '/api/countries/141/states',
responseText: {
states: [
{
id: 10,
name: "Strasbourg",
country_id: 141
}
]
}
}, {
url: '/api/countries/142/states',
responseText: {
states: [
{
id: 20,
name: "Nuevo Leon",
country_id: 142
},
{
id: 21,
name: "Chihuahua",
country_id: 142
}
]
}
});
},
afterModel() {
return Ember.RSVP.hash({
countries: this.store.findAll('country')
}).then(
(hash) => {
set(this, 'countries', hash.countries);
}
);
},
setupController(controller, model) {
this._super(controller, model);
set(controller, 'countries', get(this, 'countries'));
}
});
import Ember from 'ember';
const {
get,
set,
computed
} = Ember;
export default Ember.Controller.extend({
appName:'Ember Twiddle',
actions: {
setSelectedCountry(value) {
//var selectedCountry = this.store.peekRecord('country', value);
var selectedCountry = this.store.findRecord('country', value);
console.log(selectedCountry);
var states = selectedCountry.get('name');
console.log(states);
set(this, 'selectedCountry', selectedCountry);
return false;
}
}
});
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
states: DS.hasMany('states', {async: true})
});
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
country: DS.belongsTo('country', {async: true})
});
import DS from 'ember-data';
const ActiveModelSerializer = DS.ActiveModelSerializer;
export default ActiveModelSerializer.extend();
<h1>Welcome to {{appName}}</h1>
<br>
<br>
<h2>Country</h2>
<select onchange={{action "setSelectedCountry" value="target.value"}}>
{{#each countries as |country|}}
<option value={{country.id}}>{{country.name}}</option>
{{/each}}
</select>
<h3>States</h3>
{{#each selectedCountry.states as |state|}}
{{state.name}}
{{/each}}
{
"version": "0.4.8",
"dependencies": {
"active-model-adapter": "1.13.5",
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "1.13.11",
"ember-data": "1.13.15",
"ember-template-compiler": "1.13.11",
"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