Loading ...
Sorry, an error occurred while loading the content.

Added insertInto method, Enhanced remove method

Expand Messages
  • z_mikowski
    I posted through the TaffyDB site, but did not see an answer. Maybe it got lost, or Ian missed it. I would appreciate your thoughts, and perhaps inclusion of
    Message 1 of 2 , Jul 16, 2009
    • 0 Attachment
      I posted through the TaffyDB site, but did not see an answer. Maybe it got lost, or Ian missed it. I would appreciate your thoughts, and perhaps inclusion of these changes into TaffyDB.

      1. insertInto method : ability to insert into the taffydb array, not just append to the end; and
      2. Enhanced remove method: return the rows of data removed instead of the defunct index numbers

      (1) was pretty much required for my purposes, because I needed to adjust order based on user input. (2) I could get around in a few steps, but the approach I used prevents redundant "queries".

      Here is the patch against 1.7.2:

      535a536
      > var ary_removed = [];
      545a547
      > ary_removed.push(TOb[removeIndex[di]]);
      572c574
      < return removeIndex;
      ---
      > return ary_removed;
      576,577d577
      <
      <
      609a610,656
      > // * ADDED by mmikowski 20090712
      > // * Create public insertInto method
      > // * Purpose: this takes an obj and inserts it into
      > // * the TAFFY Obj array at the index specified
      > // *
      > // ****************************************
      > insertInto : function (newRecordObj, idxRec ){
      > var rec = (TAFFY.isArray(newRecordObj)) ? newRecordObj : [newRecordObj];
      >
      > // apply template if defined
      > var ary_insert = [];
      > if( !TAFFY.isNull(conf.template) ){
      > for ( var x=0; x<rec.length; x++ ){
      > var newObj = TAFFY.mergeObj(conf.template, rec[x]);
      > ary_insert.push( newObj );
      > }
      > }
      > else {
      > ary_insert = rec;
      > }
      >
      > // we have to slice off the array after the index,
      > // and then concat them to a new array
      > // because splice, push, etc. won't accept an array
      > var ary_end = TOb.slice( idxRec ); // slice off everything past index
      > TOb.length = idxRec; // truncate
      >
      > var TOb_new = TOb.concat( ary_insert, ary_end );
      > TOb = TOb_new;
      > delete TOb_new; // help prevent memory leaks
      >
      > this.lastModifyDate = new Date();
      >
      > // if callback function provided, execute for each insert
      > if (!T.isNull(this.onInsert)) {
      > for (var x = 0; x < rec.length; x++) {
      > this.onInsert(rec[x]);
      > }
      > }
      >
      > // rebuild TIA
      > bTIA();
      > return [TOb.length-1];
      > },
      >
      > // ****************************************
      > // *
    • z_mikowski
      New Patch against 1.7.2. Provides methods insertInto, truncate; modified remove (see prior message). Added callback stubs onInsertInto and onTruncate. 1c1
      Message 2 of 2 , Jul 18, 2009
      • 0 Attachment
        New Patch against 1.7.2. Provides methods insertInto, truncate; modified remove (see prior message). Added callback stubs onInsertInto and onTruncate.

        1c1
        < /*
        ---
        > /*!
        7,8c7
        < Version 1.7.2
        <
        ---
        > Version 1.7.2 - mod pz mmikowski
        527a527
        > // * MODIFIED by mmikowski 20090712
        531a532,535
        > // * the original version retuned the array of
        > // * indicies removed (removeIndex); the mod version
        > // * returns an array of the removed rows
        > // *
        535a540
        > var ary_removed = [];
        545a551
        > ary_removed.push(TOb[removeIndex[di]]);
        572c578
        < return removeIndex;
        ---
        > return ary_removed;
        576,577d581
        <
        <
        609a614,678
        > // * ADDED by mmikowski 20090712
        > // * Create public insertInto method
        > // * Purpose: this takes an array of objects and inserts them
        > // * into the TAFFY Obj array at the index specified
        > // *
        > // ****************************************
        > insertInto : function (newRecordObj, idxRec ){
        > var rec = (TAFFY.isArray(newRecordObj)) ? newRecordObj : [newRecordObj];
        >
        > // apply template if defined
        > var ary_insert = [];
        > if( !TAFFY.isNull(conf.template) ){
        > for ( var x=0; x<rec.length; x++ ){
        > var newObj = TAFFY.mergeObj(conf.template, rec[x]);
        > ary_insert.push( newObj );
        > }
        > }
        > else {
        > ary_insert = rec;
        > }
        >
        > // we have to slice off the array after the index,
        > // and then concat them to a new array
        > // because splice, push, etc. won't accept an array
        > var ary_end = TOb.slice( idxRec ); // slice off everything past index
        > TOb.length = idxRec; // truncate
        >
        > var TOb_new = TOb.concat( ary_insert, ary_end );
        > TOb = TOb_new;
        > delete TOb_new; // help prevent memory leaks
        >
        > this.lastModifyDate = new Date();
        >
        > // if callback function provided, execute for whole enchilada
        > if (!T.isNull(this.onInsertInto)) {
        > this.onInsertInto( newRecordObj, idxRec );
        > }
        >
        > // rebuild TIA
        > bTIA();
        > return [TOb.length-1];
        > },
        >
        > // ****************************************
        > // *
        > // * ADDED by mmikowski 20090716
        > // * Create public truncate method
        > // * Purpose: Truncates the taffy aray to the specified index
        > // *
        > // ****************************************
        > truncate : function ( idxRec ) {
        > if (!T.isNull(this.onTruncate)) {
        > this.onTruncate( TOb.slice(idxRec), idxRec );
        > }
        > TOb.length = idxRec;
        >
        > this.lastModifyDate = new Date();
        >
        > // rebuild TIA
        > bTIA();
        > return [TOb.length-1];
        > },
        >
        > // ****************************************
        > // *
        826d894
        <
        851c919
        < onInsert:null
        ---
        > onInsert:null,
        853c921,928
        < };
        ---
        > // ****************************************
        > // *
        > // * Empty On InsertInto Event - This can be replaced with a function
        > // * to call a custom action.
        > // * Passes an array of inserted rows and an index to the function.
        > // *
        > // ****************************************
        > onInsertInto:null,
        854a930,938
        > // ****************************************
        > // *
        > // * Empty On Trucate Event - This can be replaced with a function
        > // * to call a custom action.
        > // * Passes an array of inserted rows and an index to the function.
        > // *
        > // ****************************************
        > onTruncate:null,
        > };
      Your message has been successfully submitted and would be delivered to recipients shortly.