forked from BorisMoore/jquery-tmpl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for sorting, filtering, selective updating, selective
removing, of items/rows within a set of rendered templates. This is done using a static 'tmplCmd' plugin. Removed previous 'update' and 'remove' methods on template context, in favor of this approach using static methods, which allows passing one or more data items or template contexts, for removing/replacing/updating... Movie demo now includes sorting.
- Loading branch information
1 parent
a118f34
commit cd27dfc
Showing
5 changed files
with
262 additions
and
153 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* jQuery Templating Plugin Commands | ||
*/ | ||
(function(jQuery){ | ||
jQuery.extend({ | ||
tmplCmd: function( command, data, contexts ) { | ||
var retCtxs = [], before; | ||
data = jQuery.isArray( data ) ? data : [ data ]; | ||
switch ( command ) { | ||
case "find": | ||
return find( data, contexts ); | ||
case "replace": | ||
data.reverse(); | ||
} | ||
jQuery.each( contexts ? find( data, contexts ) : data, function( i, ctx ) { | ||
coll = ctx.nodes; | ||
switch ( command ) { | ||
case "update": | ||
jQuery( coll[0] ).before( ctx ); | ||
jQuery( coll ).remove(); | ||
break; | ||
case "remove": | ||
jQuery( coll ).remove(); | ||
if ( contexts ) { | ||
contexts.splice( jQuery.inArray( ctx, contexts ), 1 ); | ||
} | ||
break; | ||
case "replace": | ||
before = before ? | ||
jQuery( coll ).insertBefore( before )[0] : | ||
jQuery( coll ).appendTo( coll[0].parentNode )[0]; | ||
retCtxs.unshift( ctx ); | ||
} | ||
}); | ||
return retCtxs; | ||
function find( data, ctxs ) { | ||
var found = [], ctx, ci, cl = ctxs.length, dataItem, di = 0, dl = data.length; | ||
for ( ; di < dl; ) { | ||
dataItem = data[di++]; | ||
for ( ci = 0; ci < cl; ) { | ||
ctx = ctxs[ci++]; | ||
if ( ctx.data === dataItem ) { | ||
found.push( ctx ); | ||
} | ||
} | ||
} | ||
return found; | ||
} | ||
} | ||
}); | ||
})(jQuery); |
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
Oops, something went wrong.