Response title
This is preview!
/**
* @see https://api.jquery.com/jQuery.Callbacks/
* @see http://addyosmani.com/resources/essentialjsdesignpatterns/book/#highlighter_506612
*/
;(function($) {
var topics = [];
function getTopic(id) {
var callbacks;
topic = id && topics[id];
if (!topic) {
callbacks = $.Callbacks();
topic = {
publish: callbacks.fire,
subscribe: callbacks.add,
unsubscribe: callbacks.remove
};
if (id) topics[id] = topic;
}
return topic;
}
$.observer = {
publish: function(id) {
var args = (2 <= arguments.length) ? Array.prototype.slice.call(arguments, 1) : [];
var t = getTopic(id);
return t.publish.apply(t, args);
},
subscribe: function(id, fn) {
return getTopic(id).subscribe(fn);
},
unsubscribe: function(id, fn) {
return getTopic(id).unsubscribe(fn);
}
};
})(jQuery);
var $assets_dt = $assets_table.DataTable( {
createdRow: function ( row, data, index ) {
app.observable('assets.datatable.createrow', row, data, index);
}
} );
/**
* Color Tables functionality
*/
color_tables = {
/**
* Criteria
*
* Rules of what colors to set for what columns
*
* @todo Allow regex and the ability to apply something that allows the user to specify when a cell ISNT a value
*/
criteria: function() {
var partition_id = $( '#data-table' ).data( 'partition-id' );
var criteria = $.ajax( {
type: "GET",
url: '....smart_color.criteria.json',
async: false,
dataType: 'json'
} ).responseText;
var criteria_json = $.parseJSON(criteria);
return criteria_json[ partition_id ] || false;
},
/**
* Create Row
*
* Executes whenever the DataTables plugin initiates the tables and the rows are created (via JS, not PHP)
*
* @param row
* @param data
* @param index
*/
create_row: function( row, data, index ) {
var rules = color_tables.criteria();
var highlight_rules = [];
var i, add, classes;
if(rules) {
// Duplicate the rules array except the keys will be the column indexes in the table
$.each( rules, function ( col, r ) {
i = $( 'th#column-' + col ).index();
highlight_rules[ i ] = r;
} );
//console.log('highlight rules: ', highlight_rules);
$.each( data, function ( k, d ) {
if ( k in highlight_rules ) {
if ( $.isArray( highlight_rules[ k ].criteria ) ) {
$.each( highlight_rules[ k ].criteria, function ( sk, ss ) {
if ( data[ k ].match( new RegExp( ss ) ) ) {
//$( 'td', row ).eq( k ).addClass( highlight_rules[k].class );
if ( $.isArray( highlight_rules[ k ].classes ) ) {
classes = highlight_rules[ k ].classes.join( ' ' );
}
else {
classes = highlight_rules[ k ].classes;
}
$( 'td', row ).eq( k ).addClass( classes );
}
} );
}
else {
if ( data[ k ].match( new RegExp( highlight_rules[ k ].criteria ) ) ) {
//$( 'td', row ).eq( k ).addClass( highlight_rules[k].classes );
if ( $.isArray( highlight_rules[ k ].classes ) ) {
classes = highlight_rules[ k ].classes.join( ' ' );
}
else {
classes = highlight_rules[ k ].classes;
}
$( 'td', row ).eq( k ).addClass( classes );
}
}
}
} );
}
}
};
// The initial assets table initiation
$.observer.subscribe('assets.datatable.createrow', color_tables.create_row);
The "radio broadcast" "transmitter" is jQuery .trigger() . The "listeners" use jquery . on() as their "receiver". You send it once, and it reaches as many listeners as are listening. If there are none, there is a bit of a (small) waste.In your example, is the radio broadcast the jQuery on(), and the magazine subscription is the observable code i posted above?
I'll likely be switching to Angular sometime in the nest year or so
© 2013 jQuery Foundation
Sponsored by and others.