Last active
May 4, 2016 04:57
-
-
Save Artistan/8ee82f6753dca632ca3307917b4e6034 to your computer and use it in GitHub Desktop.
Ember CSS-STYLES helper
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 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); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
allow the hashes to be "imploded" to for css styles and bind those to the styles attribute in a handlebars template.