Created
November 8, 2013 07:53
-
-
Save lunelson/7367645 to your computer and use it in GitHub Desktop.
Mixin to Mixin passing of data using 'unique-id()' function
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
// ---- | |
// Sass (v3.3.0.rc.1) | |
// Compass (v0.13.alpha.10) | |
// ---- | |
/* | |
Using the unique-id() function to store temporary data in a global map | |
as a means to pass information to sub-routine mixins and back again | |
*/ | |
// utility function for nested map-getting | |
@function get-n($map, $keys...) { | |
@each $key in $keys { @if $map == null { @return null; } @else { $map: map-get($map, $key); } } | |
@return $map; } | |
// a global map/hash/object to store temporary data | |
$global: (); | |
// a sub-mixin which processes input data and pushes it to global under key 'id' | |
@mixin sub-mix($id, $data) { | |
$alpha: get-n($data, $id, alpha); | |
$beta: get-n($data, $id, beta); | |
$data: map-merge($data, (#{$id}: ( | |
alpha: $alpha * 2, | |
beta: $beta * 2, | |
gamma: 20 | |
))); | |
$global: map-merge($global, $data); | |
} | |
// a mixin which sends data to submixin then retrieves result via id from global | |
@mixin main-mix($alpha, $beta) { | |
$id: unique-id(); | |
$data: (#{$id}: ( | |
// create whatever here | |
alpha: $alpha, | |
beta: $beta | |
)); | |
@include sub-mix($id, $data); | |
$data: get-n($global, $id); | |
debug: inspect($data); | |
} | |
.debug { | |
@include main-mix(2,3); | |
} |
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
/* | |
Using the unique-id() function to store temporary data in a global map | |
as a means to pass information to sub-routine mixins and back again | |
*/ | |
.debug { | |
debug: (alpha: 4, beta: 6, gamma: 20); | |
} |
@ericam to follow up on this, I built a set of functions that do nested merging and setting in such a way that they can also transparently replace the native ones (argument parsing handles regular or nested cases). I built them as a recreation of map functionality for libsass using lists, but will also do a ruby sass native-maps version.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting. What's the advantage of this approach over passing the map to a function, and returning the altered map?
Also, I like your
get-n
. I built something similar I was callingsub-get
. I'd be really interested in asub-merge
ormerge-n
, allowing you to easily alter nested maps. Have any ideas on that one? :)