Last active
August 17, 2020 18:15
-
-
Save mike183/ec667248ae703707db4e2157709eae4c to your computer and use it in GitHub Desktop.
LexasCMS Blog Example
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
// =========================================================== | |
// =========================================================== | |
// | |
// This is an example of a simple blog website which has been | |
// created using the following technologies: | |
// | |
// - LexasCMS (https://www.lexascms.com) | |
// - Ember.js (https://emberjs.com/) | |
// - TailwindCSS (https://tailwindcss.com/) | |
// | |
// Source Code: https://github.com/LexasCMS/example-ember-blog | |
// Author: Michael Donaldson (Twitter: @mike_183) | |
// | |
// =========================================================== | |
// =========================================================== | |
import LexascmsAdapter from 'ember-data-lexascms/adapters/lexascms'; | |
export default class ApplicationAdapter extends LexascmsAdapter { | |
} |
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 Controller from '@ember/controller'; | |
export default class ApplicationController extends Controller { | |
appName = 'Ember Twiddle'; | |
} |
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 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; | |
} |
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 EmberRouter from '@ember/routing/router'; | |
import config from './config/environment'; | |
const Router = EmberRouter.extend({ | |
location: 'none', | |
rootURL: config.rootURL | |
}); | |
Router.map(function() { | |
this.route('page', { path: '/page/:page_num' }); | |
this.route('post', { path: '/:post_slug' }); | |
}); | |
export default Router; |
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 Route from '@ember/routing/route'; | |
import { action } from '@ember/object'; | |
export default class ApplicationRoute extends Route { | |
@action | |
didTransition() { | |
// Reset scroll position when route changes | |
window.scrollTo(0, 0); | |
} | |
} |
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 Route from '@ember/routing/route'; | |
import { inject as service } from '@ember/service'; | |
export default class IndexRoute extends Route { | |
@service store; | |
async model() { | |
// Define page size | |
const pageSize = 10; | |
// Retrieve blog posts from LexasCMS | |
const blogPosts = await this.store.query('blog-post', { | |
page: { limit: pageSize }, | |
sort: '-publishedAt', | |
include: 'coverImage' | |
}); | |
// Return | |
return { | |
featured: blogPosts.firstObject, | |
blogPosts: blogPosts.slice(1), | |
hasNextPage: blogPosts.meta.total > pageSize | |
} | |
} | |
} |
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 Route from '@ember/routing/route'; | |
import { inject as service } from '@ember/service'; | |
export default class PageRoute extends Route { | |
@service store; | |
beforeModel() { | |
// Get page number | |
const { page_num } = this.paramsFor('page'); | |
// Redirect is this is page 1 | |
if (page_num === '1') { | |
this.transitionTo('index'); | |
} | |
} | |
async model({ page_num }) { | |
// Define variables | |
const currentPage = parseInt(page_num, 10); | |
const postsOnFirstPage = 10; | |
const postsPerPage = 9; | |
// Retrieve blog posts from LexasCMS | |
const blogPosts = await this.store.query('blog-post', { | |
page: { | |
limit: postsPerPage, | |
skip: (postsPerPage * (currentPage - 2)) + postsOnFirstPage | |
}, | |
sort: '-publishedAt', | |
include: 'coverImage' | |
}); | |
// Calculate total pages | |
const totalPages = Math.ceil((blogPosts.meta.total - postsOnFirstPage) / postsPerPage) + 1; | |
// Return | |
return { | |
blogPosts, | |
nextPage: currentPage < totalPages ? currentPage + 1 : undefined, | |
prevPage: currentPage - 1 | |
}; | |
} | |
} |
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 Route from '@ember/routing/route'; | |
import { inject as service } from '@ember/service'; | |
export default class PostRoute extends Route { | |
@service store; | |
async model({ post_slug }) { | |
// Query for blog post by slug | |
const blogPost = await this.store.query('blog-post', { | |
page: { limit: 1 }, | |
filter: { | |
slug: { _eq: post_slug } | |
}, | |
include: 'author,coverImage' | |
}); | |
// Return blog post | |
return blogPost.firstObject; | |
} | |
} |
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 LexascmsSerializer from 'ember-data-lexascms/serializers/lexascms'; | |
export default class ApplicationSerializer extends LexascmsSerializer { | |
} |
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.17.1", | |
"EmberENV": { | |
"FEATURES": {}, | |
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": true, | |
"_APPLICATION_TEMPLATE_WRAPPER": false, | |
"_JQUERY_INTEGRATION": false | |
}, | |
"ENV": { | |
"lexascms": { | |
"spaceId": "7feec553-ece1-44c2-b81a-53e69625b54a" | |
} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"ember": "3.18.1", | |
"ember-template-compiler": "3.18.1", | |
"ember-testing": "3.18.1", | |
"tailwindcss": "https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" | |
}, | |
"addons": { | |
"@glimmer/component": "1.0.0", | |
"ember-cli-showdown": "4.5.0", | |
"ember-data": "3.18.0", | |
"ember-data-lexascms": "0.1.2", | |
"ember-moment": "8.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment