Skip to content

Instantly share code, notes, and snippets.

@Talleyran
Created October 17, 2011 14:23
Show Gist options
  • Save Talleyran/1292699 to your computer and use it in GitHub Desktop.
Save Talleyran/1292699 to your computer and use it in GitHub Desktop.
Sample = function( el )
{
//mixins
var me = Container( el )
//params
//,v1 = ...
//...
//dom elements
//,do_v1 = el.children( ... )
//...
//depended objects
//,ob_v1 = Func( ... )
//,ob_v2 = Refresher( Func, ... )
//,ob_v3 = Manager( els, Func, ... )
//...
//functions
//,fu_f1 = function()
// {
// ...
// }
//...
//global depended objects
//me.v1 = ob_v1
//Outer.v2 = ob_v2
//...
//global
//me.f1 = fu_f1
//...
//bindings
//...
//onload
//...
return me
}
Iter = function(opts)
{
var me = {}
,elements = [ ]
,index_hash = {}
,reverce_index_hash = {}
,max_index = 0
me.max_index = function( i ){ return max_index }
me.get_by_i = function( i ){ return elements[ i ] }
me.remove_by_id = function( i )
{
elements[ i ] = undefined
var k = index_hash[ i ]
delete index_hash[ i ]
delete reverce_index_hash[ k ]
}
me.get = function( k ){ return elements[ reverce_index_hash[ k ] ] }
me.get_key = function(v)
{
var i = elements.indexOf(v)
return index_hash[ i ]
}
me.remove = function( k ){
var i = reverce_index_hash[ k ]
delete elements[ i ]
delete index_hash[ i ]
delete reverce_index_hash[ k ]
}
me.elements = function( ){ return elements }
me.elms = me.elements
me.upgrade = function( func )
{
$.each( elements, function( i, el )
{
elements[ i ] = func( el, i, index_hash[ i ] )
}
)
}
me.each = function( func )
{
$.each( elements, function( i, el )
{
func( el, i, index_hash[ i ] )
}
)
}
me.indexing = function( func, is_redefine )
{
$.each( elements, function( i, el )
{
if( index_hash[ i ] == undefined || is_redefine ){
var k = func( el, i )
if( k!=undefined )
{
reverce_index_hash[ k ] = i
index_hash[ i ] = k
}
}
}
)
}
me.add = function( el )
{
elements[ max_index ] = el
index_hash[ max_index ] = undefined
max_index++
}
me.add_pair = function( k, v )
{
elements[ max_index ] = v
index_hash[ max_index ] = k
reverce_index_hash[ k ] = max_index
max_index++
}
me.add_array = function( arr ){
$.each( arr,
function( i, el )
{
elements[ max_index ] = el
index_hash[ max_index ] = undefined
max_index++
}
)
}
me.add_hash = function( h ){
$.each( h,
function( k, el )
{
elements[ max_index ] = el
index_hash[ max_index ] = k
reverce_index_hash[ k ] = max_index
max_index++
}
)
}
if( opts != undefined )
{
if( opts[ 'array' ]!=undefined )
{
me.add_array( opts[ 'array' ] )
}
if ( opts[ 'hash' ] != undefined )
{
me.add_hash( opts[ 'hash' ] )
}
if ( opts[ 'jq' ] != undefined )
{
me.add_array( opts[ 'jq' ] )
var attr = opts[ 'id_attr' ] ? opts[ 'id_attr' ] : 'id'
me.indexing( function( el )
{
return el.getAttribute(attr)
}
)
me.upgrade( function( el,i,k )
{
return $( el )
}
)
}
}
return me
}
ThroughRefresher = function( fabrica, el )
/*
This function add to object
methods:
refresh
reload
properties:
refresher
*/
{
var me = {}
,my_obj = null
,args = Array.prototype.slice.call(arguments)
,store = {}
args[0] = el
args.shift()
me.current = function(){
return my_obj
}
me.refresh = function(){
my_obj = fabrica.apply(null, args )
my_obj.refresh = function(){
me.refresh()
}
my_obj.reload = function( s )
{
el.html( s )
//my_obj.el().html( s )
me.refresh()
}
my_obj.refresher = me
my_obj.store = store
}
//onload
me.refresh()
return me.current
}
//alias
TR = ThroughRefresher
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment