freedombase:flashnews
Timed & localized flash messages for your Meteor app.
Getting started
Installation
meteor add freedombase:flashnews
API
APP_NEWS
1import { APP_NEWS } from 'freedombase:flashnews'
Constant for the global category of news. When put into objectType
of a flashnews it will then be part of the global app news set.
Collection
1import { FlashNewsCollection } from 'freedombase:flashnews'
You can import the collection directly like this.
Schema
1import { FlashNewsSchema } from 'freedombase:flashnews'
You can import the schema for the collection and use it in your own custom functions for validation or other purposes.
Hooks
1import { beforeFlashNewsInsert, afterFlashNewsInsert } from 'freedombase:flashnews'
Using meteor/callback-hook
, you can set these hooks to be run before and after the provided insert method.
1beforeFlashNewsInsert.register(( 2 userId, 3 content, 4 defaultLanguage, 5 startsAt, 6 endsAt, 7 objectType, 8 objectId, 9 onlyDisplayOn) => { 10 // Here check the user's credentials and return true if to proceed or false if to return unauthorized error 11 return !!userId 12})
1afterFlashNewsInsert.register(({ 2 _id: newsId, 3 content, 4 defaultLanguage, 5 userId, 6 startsAt, 7 endsAt, 8 objectType, 9 objectId, 10 onlyDisplayOn 11}) => { 12 // Returns the details of the inserted news. 13})
Method
freedombase:flashnews-create
Create a new flash news
- @param content {Object} An object with the different locales should have format like this: { en: 'First news', cs: 'První novinka' } or instead of strings can include object with your default structure for the given language.
- @param defaultLanguage {String} Default language of the news. This language will be used when the requested language is not available.
- @param startsAt {Date} The starting date when the news should be displayed, by default it is the creation date.
- @param endsAt {Date} Add a date when the news should stop being displayed, undefined by default.
- @param objectType {String} APP_NEWS by default, but you can set here your own and in combination with objectId you can for example create custom news feed for groups.
- @param objectId {String} Use in combination with objectType to specify a specific object under which to display the news.
- @param onlyDisplayOn {String[]} Only display content to languages specified in this array. If the language does not match any in this array it will not show the news.
Subscriptions
freedombase:flashnews-getMain
Gets current flash news for the site
- @param limit {Number} Limit for the return, defaults to 3
- @param language {String} Requested language of the news, defaults to
en
@returns {Mongo.Cursor}
freedombase:flashnews-getFor
Gets current flash news for the given object
- @param objectType {String}
- @param objectId {String}
- @param limit {Number} Limit for the return, defaults to 5
- @param language {String} Requested language of the news, defaults to
en
@returns {Mongo.Cursor}
FlashNews methods
Once you retrieve the news, you can call the following methods on the document.
getContent
Takes in the language you want to display the news in and returns the content given all the constraints set to it.
If the language you have requested is not available then it will use the default language, unless you have set onlyDisplayOn
.
1// subscription freedombase:flashnews-getMain 2const userLanguage = 'en' 3const news = FlashNewsCollection.find().fetch() 4const newsList = news.map((item) => { 5 return item.getContent(userLanguage) 6})
availableLanguages
Lists all the available languages for the current news based on the keys in content
.
Contributors ✨
Thanks go to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!