项目作者: davidpett

项目描述 :
Ember Data adapter for contentful.com
高级语言: JavaScript
项目地址: git://github.com/davidpett/ember-data-contentful.git
创建时间: 2016-03-23T13:44:58Z
项目社区:https://github.com/davidpett/ember-data-contentful

开源协议:MIT License

下载


Build Status
npm version
Ember Observer Score
PRs Welcome
FastBoot Ready

ember-data-contentful

This is an Ember Data adapter/serializer that uses the READ ONLY Content Delivery API from contentful

Setup in your app

  1. ember install ember-data-contentful

After installing the addon, configure your Contentful Space ID and Access Token inside ENV in config/environment.js:

  1. contentful: {
  2. space: 'YOUR-CONTENTFUL-SPACE',
  3. accessToken: 'YOUR-CONTENTFUL-ACCESS-TOKEN',
  4. previewAccessToken: 'YOUR-CONTENTFUL-PREVIEW-ACCESS-TOKEN',
  5. usePreviewApi: false,
  6. environment: 'OPTIONAL CONTENTFUL ENVIRONMENT NAME'
  7. }

You can enable the Preview API for use in development in config/environment.js:

  1. if (environment === 'development') {
  2. // ...
  3. ENV.contentful.usePreviewApi = true;
  4. }

Contentful models

Included are a few models to help with some of the default fields. Here is an example:

  1. // models/post.js
  2. import Contentful from 'ember-data-contentful/models/contentful';
  3. import attr from 'ember-data/attr';
  4. import { belongsTo, hasMany } from 'ember-data/relationships';
  5. export default Contentful.extend({
  6. author: hasMany('author'),
  7. body: attr('string'),
  8. date: attr('date'),
  9. featuredImage: belongsTo('contentful-asset'),
  10. slug: attr('string'),
  11. title: attr('string')
  12. });

will give you the default fields of contentType, createdAt, and updatedAt.

For multi-word model names, you can name your Contentful model IDs with dashes (ie. timeline-post) make sure the contentType field is inferred correctly.

For any relationship property that is a Contentful Asset (image or other media file), use the contentful-asset model. i.e. image: belongsTo('contentful-asset') in order to get the asset correctly.

Adapters and serializers

You will also need to define an adapter and serializer for your model, so that Ember Data knows to fetch data from Contentful instead of your default backend.

  1. // app/adapters/post.js
  2. import ContentfulAdapter from 'ember-data-contentful/adapters/contentful';
  3. export default ContentfulAdapter.extend({});
  1. // app/serializers/post.js
  2. import ContentfulSerializer from 'ember-data-contentful/serializers/contentful';
  3. export default ContentfulSerializer.extend({});

If you are only using Contentful models, you can set these to app/adapters/application.js and app/serializers/application.js to apply for all models.

Usage

Once you have configured your tokens and created your models, you can use the normal Ember Data requests of findRecord, findAll, queryRecord, and query. For example:

  1. model() {
  2. return this.store.findAll('project');
  3. }

or

  1. model(params) {
  2. return this.store.findRecord('project', params.project_id);
  3. }

If you want to use pretty urls and the slug field in contentful, you can make your query like so:

  1. model(params) {
  2. return this.store.queryRecord('page', {
  3. 'fields.slug': params.page_slug
  4. });
  5. },
  6. serialize(model) {
  7. return { page_slug: get(model, 'slug') };
  8. }

and ensure that you declare your route in router.js like this:

  1. this.route('page', { path: ':page_slug' });

Previewing Content

Contentful provides a Preview API that allows you to preview unpublished content. In order to enable this, ensure you have your previewAccessToken configured in config/environment.js and enable the usePreviewApi property.

For more information on the contentful Content Delivery API and the available queries, look here: https://www.contentful.com/developers/docs/references/content-delivery-api/