Last active
April 18, 2020 05:50
-
-
Save JedWatson/11218381 to your computer and use it in GitHub Desktop.
Example of how to use the filters option for Relationship fields
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
// 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'] | |
}]; |
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
// 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(); |
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
// 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
@JedWatson what if I want to get the list of countries from a database? How to asynchronously get data from the database before keystone start?