Skip to content

Instantly share code, notes, and snippets.

@Artistan
Last active May 4, 2016 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Artistan/8ee82f6753dca632ca3307917b4e6034 to your computer and use it in GitHub Desktop.
Save Artistan/8ee82f6753dca632ca3307917b4e6034 to your computer and use it in GitHub Desktop.
Ember CSS-STYLES helper
import Ember from 'ember';
/*
Template:
<div style={{css-styles background-url='url("this/is/my/url")' color="red"}}>My Fill Color</div>
OR
<li id="{{index}}" style={{css-styles background-url=(concat 'url("' photo.thumbnail '")') }}></li>
*/
export function cssStyles(params, hash) {
let styles = '';
if(params !== undefined){
styles = joinStyles(params);
}
if(hash !== undefined){
styles = joinStyles(hash);
}
return Ember.String.htmlSafe(styles);
}
export default Ember.Helper.helper(cssStyles);
/*
var options = { color : 'red', border : 'solid gray 1px', font-size : '12px'};
joinStyles(options);
> "color:red;border:solid gray 1px;font-size:12px;
joinStyles(options,'=>', ' '));
> "id=>1 name=>lucas country=>brasil"
*/
var joinStyles = function(object, glue, separator) {
if (glue === undefined) {
glue = ':';
}
if (separator === undefined) {
separator = ';';
}
return $.map(Object.getOwnPropertyNames(object), function(k) {
return [k, object[k]].join(glue);
}).join(separator);
};
@Artistan
Copy link
Author

Artistan commented May 4, 2016

allow the hashes to be "imploded" to for css styles and bind those to the styles attribute in a handlebars template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment