npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-data-lexascms

v0.1.4

Published

The officially supported addon for using LexasCMS with Ember Data.

Downloads

3

Readme

ember-data-lexascms

This is the officially supported addon for using the LexasCMS JSON:API content delivery API with Ember Data.

Table of Contents

Why do I need this addon?

While LexasCMS does natively support JSON:API and can be used with Ember Data without this addon, the addon defines some helpful defaults and provides predefined models in order to improve developer experience.

The addon currently provides the following:

  • Preconfigured adapater/serializer
  • Predefined model for retrieving image fields
  • lexascms service for managing the LexasCMS request context

Installation

ember install ember-data-lexascms

Usage

You'll need a LexasCMS account before you can use this addon. If you don't have one already, you can click here to create one.

After you've installed the addon, add the following config to your applications config/environment.js file.

let ENV = {
  // ...

  lexascms: {
    spaceId: 'YOUR_LEXASCMS_SPACE_ID',
    apiKey: 'YOUR_LEXASCMS_API_KEY' // Optional, unless using content previews
  }

  // ...
};

Warning: Placing API keys in your config/environment.js file will cause them to be publicly exposed. When using content previews, we recommend using private environments which cannot be accessed publicly.

Configure Adapters and Serializers

In order to have Ember Data pull content from LexasCMS, you'll also need to define some customer adapters and serializers.

If there are only specific models which are to be retrieved from LexasCMS, you'll need to define a custom adapter and serializer for each model.

The below examples defines an adapter and serializer for the blog-post model.

// File: app/adapters/blog-post.js

import LexasCMSAdapter from 'ember-data-lexascms/adapters/lexascms';

export default class BlogPostAdapter extends LexasCMSAdapter {
}
// File: app/serializers/blog-post.js

import LexasCMSSerializer from 'ember-data-lexascms/serializers/lexascms';

export default class BlogPostSerializer extends LexasCMSSerializer {
}

Alternatively, if all of your models are to be retrieved from LexasCMS, you can just define a custom application adapter and serializer. These will then be used for all of the models within your application.

// File: app/adapters/application.js

import LexasCMSAdapter from 'ember-data-lexascms/adapters/lexascms';

export default class ApplicationAdapter extends LexasCMSAdapter {
}
// File: app/serializers/application.js

import LexasCMSSerializer from 'ember-data-lexascms/serializers/lexascms';

export default class ApplicationSerializer extends LexasCMSSerializer {
}

Retrieving Image Fields

In LexasCMS, image fields are treated as relationships. When defining a model which contains an image field, you should create a belongsTo relationship to the predefined core-image model.

The below example shows how you could define a blog-post model which contains an image field called coverImage:

import Model, { attr, belongsTo } from '@ember-data/model';

export default class BlogPostModel extends Model {

  @attr slug;
  @attr title;
  @attr publishedAt;
  @attr excerpt;
  @attr mainContent;
  
  @belongsTo('author') author;
  @belongsTo('core-image') coverImage;

}

Retrieving and Querying Content

Once you have completed the above steps and defined all of your models, you can retrieve content from LexasCMS using the regular Ember Data methods (findAll, findRecord, query etc.).

If you would like to apply filters or sorting options to your query, you can provide those options by using Ember Data's query method like so:

this.store.query('blog-post', {

  // Fitering
  filter: {
    publishedAt: { _gte: '2020-01-01' }
  },

  // Sorting
  sort: '-publishedAt'
  
});

For further information on the available query options, please see the full documentation for the JSON:API content delivery API.

Setting the Request Context

In the event that you would like to set a request context on your requests to LexasCMS (i.e. for content personalisation), you can call the setRequestContext method on the lexascms service.

This method can be called from anywhere within your application, and will automatically attach the provided context to all requests made to LexasCMS via Ember Data.

Note: You can also retrieve the current request context using the getRequestContext method on the lexascms service.

The following example shows how you could attach a request context from the beforeModel hook of your application route.

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default class ApplicationRoute extends Route {

  @service lexascms;

  beforeModel() {
    this.lexascms.setRequestContext({
      audienceAttributes: {
        age: 25,
        location: 'GB'
      }
    });
  }

}

Supporting Content Previews

When making use of LexasCMS's visual content previews feature, LexasCMS will load your application with the lexascmsRequestContent query parameter.

This value of this parameter will be a pre-encoded request context, which should be provided directly to all request to the Content Delivery API.

The code snippet below shows an example of how you could achieve this using your Application route.

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default class ApplicationRoute extends Route {

  @service lexascms;

  queryParams = {
    lexascmsRequestContext: {
      refreshModel: true
    }
  };

  model({ lexascmsRequestContext }) {
    this.lexascms.setRequestContext(lexascmsRequestContext);
  }

}

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.