function bindDevicePopupElements(document)
{
    // ------------ select2 group search ----
    var parents = document.parents ? document.parents : [];

    // get group objects for groups that this device directly belongs to
    var belongsToGroups = Groups.find({_id: {$in: parents}}).fetch();

    var select2options = {
        multiple: true,
        placeholder: "Select groups.",
        // when the user types
        query: function (query) {

            // build regex to find text anywhere in field
            var expression = ".*"+query.term+".*";
            var rx = RegExp(expression,'i');

            // search mongo
            var documents = Groups.find({name:rx}).fetch();

            // callback is expecting a results member
            var data = {
                results: convertDocumentsSelect2(documents)
            };

            query.callback(data);
        },
        initSelection: function(element, callback) {
            // don't care what element is bc we already know

            // callback is just expecting an array
            callback(convertDocumentsSelect2(belongsToGroups));
        }
    };

    var select2groupSearchSelector = '#select2-device-popup-group-input-' + document._id;

    // shorten name
    var $select2group = $(select2groupSearchSelector);

    // build select2
    $select2group.select2(select2options);

    // WHAT IS WRONG? we need this line, but it has no data.  The data came from initSelection()
    $select2group.select2("val", []);

    // bind change
    $select2group.on("change", function(e) {
        // here e has a lot of stuff in it. including e.val which is an array of the full set
        // we only deal in deltas tho

        if( e && e.added && e.added.id ) {
            Devices.update(document._id, {$addToSet:{'parents': e.added.id}});
        }

        if( e && e.removed && e.removed.id ) {
            Devices.update(document._id, {$pull:{'parents': e.removed.id}});
        }
    });
}
// Called right after a device popup is rendered and also added to the DOM
Called right after a device popup is rendered and also added to the DOM
Called right after a device popup is rendered and also added to the DOM
function bindDevicePopupElements(document)
{
    var parents = document.parents ? document.parents : [];
    var belongsToGroups = Groups.find({_id: {$in: parents}}).fetch();
    var select2options = {
        multiple: true,
        placeholder: "Select groups.",
        query: function (query) {
            var expression = ".*"+query.term+".*";
            var rx = RegExp(expression,'i');
            var documents = Groups.find({name:rx}).fetch();
            var data = {
                results: convertDocumentsSelect2(documents)
            };
            query.callback(data);
        },
        initSelection: function(element, callback) {
            callback(convertDocumentsSelect2(belongsToGroups));
        }
    };
    var select2groupSearchSelector = '#select2-device-popup-group-input-' + document._id;
    var $select2group = $(select2groupSearchSelector);
    $select2group.select2(select2options);
    $select2group.select2("val", []);
    $select2group.on("change", function(e) {
        if( e && e.added && e.added.id ) {
            Devices.update(document._id, {$addToSet:{'parents': e.added.id}});
        }
        if( e && e.removed && e.removed.id ) {
            Devices.update(document._id, {$pull:{'parents': e.removed.id}});
        }
    });
}
function bindDevicePopupElements ( document ) { var parents = document . parents ? document . parents : [ ] ; var belongsToGroups = Groups . find ( { _id : { $in : parents } } ) . fetch ( ) ; var select2options = { multiple : true , placeholder : "Select groups." , query : function ( query ) { var expression = ".*" + query . term + ".*" ; var rx = RegExp ( expression , 'i' ) ; var documents = Groups . find ( { name : rx } ) . fetch ( ) ; var data = { results : convertDocumentsSelect2 ( documents ) } ; query . callback ( data ) ; } , initSelection : function ( element , callback ) { callback ( convertDocumentsSelect2 ( belongsToGroups ) ) ; } } ; var select2groupSearchSelector = '#select2-device-popup-group-input-' + document . _id ; var $select2group = $ ( select2groupSearchSelector ) ; $select2group . select2 ( select2options ) ; $select2group . select2 ( "val" , [ ] ) ; $select2group . on ( "change" , function ( e ) { if ( e && e . added && e . added . id ) { Devices . update ( document . _id , { $addToSet : { 'parents' : e . added . id } } ) ; } if ( e && e . removed && e . removed . id ) { Devices . update ( document . _id , { $pull : { 'parents' : e . removed . id } } ) ; } } ) ; }
Called right after a device popup is rendered and also added to the DOM
Called right after a device popup is rendered and also added to the DOM
// ------------ select2 group search ---- // get group objects for groups that this device directly belongs to // when the user types // build regex to find text anywhere in field // search mongo // callback is expecting a results member // don't care what element is bc we already know // callback is just expecting an array // shorten name // build select2 // WHAT IS WRONG? we need this line, but it has no data.  The data came from initSelection() // bind change // here e has a lot of stuff in it. including e.val which is an array of the full set // we only deal in deltas tho