user6618353
user6618353

Reputation:

Global Variable in meteor client javascript

I have this code to retrieve array datas from my collection. I managed to split them in other to get the data of each position in the array but now I need each of this data for another query, When i make the query directly after the split I get errors, is there a way of saving the variable mySplitResult[] as a global variable in other to access it from another template helper to make my queries or is there any other way to use the result from mySplitResult for query in the same function. All this am doing in javascript from the client side.

Template.testeo.helpers({
  ls: function(){
    var list=Calender.find({status_visualizacion: "visible"});
    var count = 0;
    list.forEach(function(calender){
      var result =  + count + "," + calender.calendario_slaves;
      mySplitResult = result.split(",");
      var i = 0;
      while (i < mySplitResult.length){
        //console.log(mySplitResult[i]);
        trozo= mySplitResult[i];
        console.log(trozo);
        i++;
        //return trozo;
      }
      count += 1;

    });
  }
}); 

Upvotes: 0

Views: 176

Answers (2)

Piyush Thapa
Piyush Thapa

Reputation: 206

well if you want the variable to be global and not reactive then just insert into window variable and access from other template

window.Myvar = x

Upvotes: 1

CodeChimp
CodeChimp

Reputation: 8154

There are several ways this could be accomplished. One would be to use the Session object to store the values. This is dangerous, though, as Session lives for, well, the length of the user's session. Many of strange bugs and anomalies have been attributed to code that relies too much on Session as you can get into strange states that are unexpected or not well thought out.

The other way would be to pass the values into your template using the route path variables or query params. I prefer route path variables myself, as it's more of a "RESTful" feel to do something like app.com/users/codechimp, where codechimp in this example is the user ID I want to view in my app. In order to do this right, you need to give some thought to how to best utilize the URL patterns. I feel this is a better approach typically as it doesn't tie the state to a variable that is shared amongst your code, which in turn makes it less susceptible (read near impossible) to bugs that stem from shared session variables.

Upvotes: 0

Related Questions