append class from a var

4 views
Skip to first unread message

Ezequiel Gentile Montes

unread,
Nov 27, 2016, 1:46:00 PM11/27/16
to JavaScript Templates Engine PURE
HI I have this

var cssColors = ['Red', 'Pink', 'Purple', 'Deep-Purple', 'Indigo', 'Blue',
'Light-Blue', 'Cyan', 'Teal', 'Green', 'Light-Green',
'Lime', 'Yellow', 'Amber', 'Orange', 'Deep-Orange', 'Brown',
'Grey', 'Blue-Grey'];
var rand = cssColors[Math.floor(Math.random() * cssColors.length)]

and I want something like

var directive = {
  '.col-md-4':{
    'user<-':{
      'article@class+' : '#{rand}',
      'span': 'user.name
}

how can I call rand variable from outside data object?

Mic (BeeBole)

unread,
Nov 27, 2016, 3:11:31 PM11/27/16
to JavaScript Templates Engine PURE
If you want to share some javascript code(eg: a utility function) or variables(like cssColors) at render time, you must make a closure, using a function.
The function can share variables out of its scope.


var cssColors = ...

...

 
'article@class+': function(){
   
return cssColors[Math.floor(Math.random() * cssColors.length)];
 
}

...

Ezequiel Gentile Montes

unread,
Nov 27, 2016, 7:25:52 PM11/27/16
to JavaScript Templates Engine PURE
oh ok! I tried that but because of a lack of space after append I was doing it wrong...
 'article@class+': " " + function(){
    return cssColors[Math.floor(Math.random() * cssColors.length)];
  
}

and this is how it works
'article@class+': function(){
    
return " " + cssColors[Math.floor(Math.random() * cssColors.length)];
  
}

thanks again Mic.
Reply all
Reply to author
Forward
0 new messages