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

getByindex method?

Expand Messages
  • paulvoors
    Is there a getByIndex method for Taffydb? Such a method would be very usefull in combination with the find method. The find method returns an array of the
    Message 1 of 5 , Apr 17, 2009
    • 0 Attachment
      Is there a getByIndex method for Taffydb? Such a method would be very usefull in combination with the find method. The find method returns an array of the index values of the objects in the array. With the "getByIndex(index)" method (or something alike) you could easily retrieve the object belonging to that index.
    • tacoman_cool
      Actually, that is built right into .get() All of these should be valid: collection.get({field: somevalue }); collection.get(0); collection.get([0,1,2,3]); As a
      Message 2 of 5 , Apr 17, 2009
      • 0 Attachment
        Actually, that is built right into .get()

        All of these should be valid:

        collection.get({field:"somevalue"});
        collection.get(0);
        collection.get([0,1,2,3]);

        As a general rule methods are overloaded to take either indexes or objects to lookup records. You can, for example, remove by passing indexes or a filter objects or update by doing the same thing.

        So collection.get(collection.find({field:"somevalue"})); is also valid.

        --- In taffydb@yahoogroups.com, "paulvoors" <paulvoors@...> wrote:
        >
        > Is there a getByIndex method for Taffydb? Such a method would be very usefull in combination with the find method. The find method returns an array of the index values of the objects in the array. With the "getByIndex(index)" method (or something alike) you could easily retrieve the object belonging to that index.
        >
      • berg.matt
        I also use collection.first quite a bit with collection.find, as I am usually searching for stuff by an ID.
        Message 3 of 5 , Apr 17, 2009
        • 0 Attachment
          I also use collection.first quite a bit with collection.find, as I am usually searching for stuff by an ID.

          --- In taffydb@yahoogroups.com, "tacoman_cool" <ian@...> wrote:
          >
          > Actually, that is built right into .get()
          >
          > All of these should be valid:
          >
          > collection.get({field:"somevalue"});
          > collection.get(0);
          > collection.get([0,1,2,3]);
          >
          > As a general rule methods are overloaded to take either indexes or objects to lookup records. You can, for example, remove by passing indexes or a filter objects or update by doing the same thing.
          >
          > So collection.get(collection.find({field:"somevalue"})); is also valid.
          >
          > --- In taffydb@yahoogroups.com, "paulvoors" <paulvoors@> wrote:
          > >
          > > Is there a getByIndex method for Taffydb? Such a method would be very usefull in combination with the find method. The find method returns an array of the index values of the objects in the array. With the "getByIndex(index)" method (or something alike) you could easily retrieve the object belonging to that index.
          > >
          >
        • paulvoors
          That helps, thank you for the answer. But I still have a problem handling the result. Here is some code: var favorList = linksStore.get(2);
          Message 4 of 5 , Apr 20, 2009
          • 0 Attachment
            That helps, thank you for the answer. But I still have a problem handling the result. Here is some code:

            var favorList = linksStore.get(2);
            alert(linksStore.get(2)); //(1) Result: [0bject, object]
            alert(linksStore.get(2).linkUrl); //(2)Result: undefined.

            var newList = document.createElement("ul");
            // loop over each record using TaffyDB's forEach, add new row
            favorList.forEach(function(r, c){
            alert("r = "r) //Result: r = [Object, Object]
            var newListItem = document.createElement("li");
            var newLink = document.createElement("a");
            newLink.setAttribute('href',"http://" + r.linkUrl); //(3) Result: OK! The url is shown.
            newLink.setAttribute('title',r.description);
            newLink.setAttribute('target','_new');
            newLink.appendChild(document.createTextNode(r.title));
            newListItem.appendChild(newLink);
            newList.appendChild(newListItem);
            });

            Result number (2), does not work as I expect (returning an url). But number (4) works, while (1) and (2) are both [Object, Object].
            What is wrong? Or how can I read out the fields of the object returned by 'collection.get(2)'?

            While writing this message I found it out myself!. But submitted it anyway as it might be helpful for other developers:

            Get returns always an array, so you should at [0], even when you are sure there is only one result:
            alert(linksStore.get(2)[0].linkUrl); //(2)Result: www.tryllian.com.


            Paul Voors




            --- In taffydb@yahoogroups.com, "tacoman_cool" <ian@...> wrote:
            >
            > Actually, that is built right into .get()
            >
            > All of these should be valid:
            >
            > collection.get({field:"somevalue"});
            > collection.get(0);
            > collection.get([0,1,2,3]);
            >
            > As a general rule methods are overloaded to take either indexes or objects to lookup records. You can, for example, remove by passing indexes or a filter objects or update by doing the same thing.
            >
            > So collection.get(collection.find({field:"somevalue"})); is also valid.
            >
            > --- In taffydb@yahoogroups.com, "paulvoors" <paulvoors@> wrote:
            > >
            > > Is there a getByIndex method for Taffydb? Such a method would be very usefull in combination with the find method. The find method returns an array of the index values of the objects in the array. With the "getByIndex(index)" method (or something alike) you could easily retrieve the object belonging to that index.
            > >
            >
          • tacoman_cool
            Another option is to use first(). I built the various methods to always return the same type of data to allow people to program predictably without having to
            Message 5 of 5 , Apr 20, 2009
            • 0 Attachment
              Another option is to use first(). I built the various methods to always return the same type of data to allow people to program predictably without having to check types (arrays vs objects, for example). If you know you will always have one record then you can use first() as a short cut to get()[0].


              --- In taffydb@yahoogroups.com, "paulvoors" <paulvoors@...> wrote:
              >
              > That helps, thank you for the answer. But I still have a problem handling the result. Here is some code:
              >
              > var favorList = linksStore.get(2);
              > alert(linksStore.get(2)); //(1) Result: [0bject, object]
              > alert(linksStore.get(2).linkUrl); //(2)Result: undefined.
              >
              > var newList = document.createElement("ul");
              > // loop over each record using TaffyDB's forEach, add new row
              > favorList.forEach(function(r, c){
              > alert("r = "r) //Result: r = [Object, Object]
              > var newListItem = document.createElement("li");
              > var newLink = document.createElement("a");
              > newLink.setAttribute('href',"http://" + r.linkUrl); //(3) Result: OK! The url is shown.
              > newLink.setAttribute('title',r.description);
              > newLink.setAttribute('target','_new');
              > newLink.appendChild(document.createTextNode(r.title));
              > newListItem.appendChild(newLink);
              > newList.appendChild(newListItem);
              > });
              >
              > Result number (2), does not work as I expect (returning an url). But number (4) works, while (1) and (2) are both [Object, Object].
              > What is wrong? Or how can I read out the fields of the object returned by 'collection.get(2)'?
              >
              > While writing this message I found it out myself!. But submitted it anyway as it might be helpful for other developers:
              >
              > Get returns always an array, so you should at [0], even when you are sure there is only one result:
              > alert(linksStore.get(2)[0].linkUrl); //(2)Result: www.tryllian.com.
              >
              >
              > Paul Voors
              >
              >
              >
              >
              > --- In taffydb@yahoogroups.com, "tacoman_cool" <ian@> wrote:
              > >
              > > Actually, that is built right into .get()
              > >
              > > All of these should be valid:
              > >
              > > collection.get({field:"somevalue"});
              > > collection.get(0);
              > > collection.get([0,1,2,3]);
              > >
              > > As a general rule methods are overloaded to take either indexes or objects to lookup records. You can, for example, remove by passing indexes or a filter objects or update by doing the same thing.
              > >
              > > So collection.get(collection.find({field:"somevalue"})); is also valid.
              > >
              > > --- In taffydb@yahoogroups.com, "paulvoors" <paulvoors@> wrote:
              > > >
              > > > Is there a getByIndex method for Taffydb? Such a method would be very usefull in combination with the find method. The find method returns an array of the index values of the objects in the array. With the "getByIndex(index)" method (or something alike) you could easily retrieve the object belonging to that index.
              > > >
              > >
              >
            Your message has been successfully submitted and would be delivered to recipients shortly.