Slug behavior for firenze.js.
Automatically generate slugs (SEO friendly strings) when creating new records.
$ npm install --save firenze-behavior-slug
$ bower install --save firenze-behavior-slug
When saving a new model, it will automatically generate a slug and set it to specified field for you.
For example, when saving a post with the title Hello World
:
var posts = new Posts();
var post = posts.model({
title: 'Hello World'
});
post.save().then(function (model) {
var slug = model.get('slug'); // `hello-world`
});
It will also save the value hello-world
in slug
field.
With npm:
$ npm install --save firenze-behavior-slug
Now you can require it as follows:
var SlugBehavior = require('firenze-behavior-slug');
// create your Database instance...
db.createCollection({
behaviors: [
SlugBehavior
]
});
If you want to pass extra configuration options:
db.createCollection({
behaviors: [
{
'class': SlugBehavior
options: {
source: 'title', // field to convert
field: 'slug', // field to store the slug in
separator: '-',
}
}
]
});
Or Bower:
$ bower installl --save firenze-behavior-slug
Can be loaded in your HTML page as follows:
<script src="bower_components/lodash/lodash.min.js"></script>
<script src="bower_components/async/lib/async.js"></script>
<script src="bower_components/bluebird/js/browser/bluebird.min.js"></script>
<script src="bower_components/validator-js/validator.min.js"></script>
<script src="bower_components/firenze/dist/firenze.min.js"></script>
<script src="bower_components/firenze-behavior-slug/dist/firenze-behavior-slug.min.js"></script>
<script>
// Slug behavior is available in `firenze.SlugBehavior`
</script>
Tests are written with mocha, and can be run via npm:
$ npm test
MIT © Fahad Ibnay Heylaal