Login Register

pulling items out of a store in vars

Please forgive beginner question, all alone in rural zone
no one knows anything about dojo

http://colleenweb.com/dtdg-CAFEkids.html

Am fooling with example in Matt's book on page 208. I am getting the store OK, all the commented code works. It is only
when I try to bind a specific item to a local variable.
I get error msg from Firebug
Using FF3 Ubuntu
I need to be able to do this because the remaining examples use this technique to start writing to datastore.
My code looks just like in the book AFAIK. It is this line:

console.log(coffeeStore.getValue(spanishCoffeeItem, "name"));

that is giving issue. If I comment that, everything is hunkie dorie. The error is in the dojo.js file line 95

dojo.data.ItemFileReadStore: Invalid item argument.
[Break on this error] undefined

If it's some kind of scope error I dont get it. I thought in javascrip if you refer to vars that have been declare with "var" inside a function . Plus it come right out of the book. Thank you

data stores

Data stores are asynchronous so the query to the store is still executing by the time it gets to that line.

The variable is in scope, it just isn't an item from the data store so the data store throws the error you see. You can see it in action if you change the commented line above to a simple output like 'console.log("O HAI")'. On the FB console, you'll see (depending on your machine):

O HAI
Spanish

which shows you that you get to that point before the var is instantiated as an item. So, there's no guarantee that the variable is properly instantiated by the time you get to that bit of code.

I usually pass the item to another function that uses the item to do something when I'm using datastores for this reason.

Also, the page you linked to threw other errors for me besides that one mentioned above. djConfig should be defined before the call to dojo.js. And, your entire code block after the dojo.require should be in a dojo.addOnLoad so that the require is finished before your code begins executing. I posted an updated version here so you can see the changes I made:
http://pastebin.com/m1ca03e22

Hope this helps.

Thank you very much

that pastebin website is awesome. Your version works excellent
and I see what the problem is with it not being ready because the datastore is asynchronous.

All the other tests I made have everything wrapped in addOnLoad I think it was a late night mistake on my part to leave it off.

So now you have it wrapped in addOnLoad and OK But Still I need to refer to the variable spanishCoffeeItem some time though
How and where to put this line console.log(coffeeStore(getValue(spanishCoffeeItem,"name")));
so that its guarantee to be finished instanciating.
l

You can reference the

You can reference the item(s) returned from the fetch in the functions defined in the "onItem" and/or "onComplete" parameters.

An example using "onComplete" (which will get an array of all the items found):

var spanishCoffeeItem = undefined;
coffeeStore.fetch({
    query:  {name: "Spanish"},
    onComplete:  function(items) {
      if (items.length == 1) {
        spanishCoffeeItem = items[0];
        doSomething();
      }
      else {
        // what to do if more than one spanish coffee item?
        console.log("O NOES!!");
      }
    },
    onError: function(item,request) {
     console.log("Snafu");
    }
});

function doSomething() {
  dojo.byId("someDiv").innerHTML = coffeeStore.getValue(spanishCoffeeItem,"name");
  console.log('sumpin ' + spanishCoffeeItem);
  doSomethingElse();
}

function doSomethingElse() {
  dojo.byId('someOtherDiv").innerHTML = coffeeStore.getValue(spanishCoffeeItem,"price");
  console.log('
sumpin else ' +  spanishCoffeeItem);
}

Note that you could also pass the spanishCoffeeItem to each of the functions by changing their definitions and arguments. I typed this off of the top of my head so it may or may not run as is. But you should get the idea of what can be done.

There are other ways to do this (like with a dojo.publish/subscribe model or listeners) but this way is the quickest and most easily understood I feel.

Hope this helps.

OK. Thank you

I get this pretty much. I don't think console.log('sumpin' + spanishCoffeeItem) will work, I think you will actually need
console.dir for that cuz the spanishCoffeeItem is not just a string. ;) But I get what you're saying with it.

However....
you really wouldn't need to save it in a var if all ur gong to do is pass to a fcn right there. You could have

onComplete: function(items) {
if (items.length == 1) {
//spanishCoffeeItem = items[0];
doSomething(items[0]);
}
...
}

and then your doSomething function would take a parameter, as u suggested.

The thing is Matt's book on page 208 has code where it did var spanishCoffeeItem and saved it,
then used it directly in code later. That code felt funny to me from the get go but it's in the book.