Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
Revert "Add data-each var and value to the scope of conditionals and …
Browse files Browse the repository at this point in the history
…graphie code blocks so they evaluate even outside of data-each context. (fix #149)"

This reverts commit 0cede38.
  • Loading branch information
marcia committed Sep 21, 2011
1 parent e63e3dd commit da42824
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions utils/tmpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ jQuery.tmpl = {
"data-if": function( elem, value ) {
var $elem = jQuery( elem );

// Check if the attribute should be deleted
if ( $elem.data( "toDelete" ) ) {
$elem.removeAttr( "data-if" ).removeData( "if" );
}

value = value && jQuery.tmpl.getVAR( value );

// Save the result of this data-if in the next sibling for data-else-if and data-else
Expand All @@ -32,6 +37,11 @@ jQuery.tmpl = {
"data-else-if": function( elem, value ) {
var $elem = jQuery( elem );

// Check if the attribute should be deleted
if ( $elem.data( "toDelete" ) ) {
$elem.removeAttr( "data-else-if" ).removeData( "elseIf" );
}

var lastCond = $elem.data( "lastCond" );

// Show this element iff the preceding element was hidden AND this data-if returns truthily
Expand All @@ -49,6 +59,11 @@ jQuery.tmpl = {
"data-else": function( elem ) {
var $elem = jQuery( elem );

// Check if the attribute should be deleted
if ( $elem.data( "toDelete" ) ) {
$elem.removeAttr( "data-else" ).removeData( "else" );
}

if ( $elem.data( "lastCond" ) ) {
// Delete the element if the data-if of the preceding element was true
return [];
Expand Down Expand Up @@ -321,23 +336,9 @@ jQuery.fn.tmpl = function() {
var clone = jQuery( elem ).clone( true )
.removeAttr( "data-each" ).removeData( "each" )[0];

// Prepend all conditional statements with a declaration of ret.value
// and an assignment of its current value so that the conditional will
// still make sense even when outside of the data-each context
var conditionals = [ "data-if", "data-else-if", "data-else" ];
for ( var i = 0; i < conditionals.length; i++ ) {
var conditional = conditionals[i];
jQuery( clone ).find( "[" + conditional + "]" ).each(function() {
var code = jQuery( this ).attr( conditional );
code = "(function() { var " + ret.value + " = " + value + "; return " + code + " })()";
jQuery( this ).attr( conditional, code );
});
}

// Do the same for graphie code
jQuery( clone ).find( ".graphie" ).each(function() {
var code = jQuery( this ).text();
jQuery( this ).text( "var " + ret.value + " = " + value + ";" + code );
// Flag elements with the following attributes so that the attributes can be removed after templating
jQuery( clone ).find("[data-if], [data-else-if], [data-else]").each(function() {
jQuery( this ).data( "toDelete", true );
});

// Insert in the proper place (depends on whether the loops is the last of its siblings)
Expand Down

0 comments on commit da42824

Please sign in to comment.