-
-
Save benoror/1bfdc2a284e0a8d67b8f06f792017803 to your computer and use it in GitHub Desktop.
Country/States Cascaded
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'; | |
const ActiveModelAdapter = DS.ActiveModelAdapter; | |
const ApplicationAdapter = ActiveModelAdapter.extend({ | |
shouldReloadAll: function(store, snapshotRecordArray) { | |
return true; | |
}, | |
shouldBackgroundReloadRecord: function(store, snapshot) { | |
return false; | |
} | |
}); | |
export default ApplicationAdapter; |
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 ApplicationAdapter from './application'; | |
const CountryAdapter = ApplicationAdapter.extend({ | |
namespace: 'api' | |
}); | |
export default CountryAdapter; |
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 ApplicationAdapter from './application'; | |
const StateAdapter = ApplicationAdapter.extend({ | |
namespace: 'api', | |
}); | |
export default StateAdapter; |
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'; | |
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", | |
state_ids: [10] | |
}, | |
{ | |
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/states/10', | |
responseText: { | |
state: { | |
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 | |
} | |
] | |
} | |
}); | |
}, | |
model() { | |
return this.store.findAll('country'); | |
} | |
}); |
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'; | |
const { | |
get, | |
set, | |
computed | |
} = Ember; | |
export default Ember.Controller.extend({ | |
appName:'Ember Twiddle', | |
countries: Ember.computed.alias('model'), | |
actions: { | |
setSelectedCountry(countryId) { | |
var selectedCountry = this.get('model').findBy('id', countryId); | |
console.log(selectedCountry); | |
window.selectedCountry = selectedCountry; | |
selectedCountry.get('states').then(() => { | |
var states = selectedCountry.get('name'); | |
set(this, 'selectedCountry', selectedCountry); | |
}); | |
return false; | |
} | |
} | |
}); |
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'), | |
states: DS.hasMany('state', {async: 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 DS from 'ember-data'; | |
export default DS.Model.extend({ | |
name: DS.attr('string'), | |
country: DS.belongsTo('country', {async: 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 DS from 'ember-data'; | |
const ActiveModelSerializer = DS.ActiveModelSerializer; | |
export default ActiveModelSerializer.extend(); |
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.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