Skip to content

Instantly share code, notes, and snippets.

@VinayaSathyanarayana
Forked from JedWatson/Countries.js
Created July 17, 2017 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VinayaSathyanarayana/4d4a51d6d124592be1e2e5c28e3bb4cf to your computer and use it in GitHub Desktop.
Save VinayaSathyanarayana/4d4a51d6d124592be1e2e5c28e3bb4cf to your computer and use it in GitHub Desktop.
Example of how to use the filters option for Relationship fields
// A global file to provide the countries and cities
exports.countries = [{
name: 'Australia',
cities: ['Melbourne', 'Sydney', 'Canberra']
}, {
name: 'España',
cities: ['Madrid', 'Barcelona', 'Sevilla']
}, {
name: 'Italia',
cities: ['Roma', 'Venecia', 'Turin']
}];
// Shorthand code to create models with a filtered relationship.
var _ = require('underscore'),
countries = require('countries').countries;
new keystone.List('City').add({
name: String,
country: { Types.Select, options: _.pluck(countries, 'name') }
}).register();
new keystone.List('Person').add({
name: String,
country: { Types.Select, options: _.pluck(countries, 'name') },
city: { type: Types.Relationship, ref: 'City', filters: { country: ':country' }
}).register();
// Example of an update script that will populate the Cities model
var _ = require('underscore'),
countries = require('countries').countries;
var cities = exports.cities = _.flatten(countries.map(function(country) {
return country.cities.map(function(city) {
return { country: country.name, name: city.name };
});
}));
exports.create = {
City: cities
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment