0

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;

    });
  }
}); 
2
  • No it's not possible to access any global javascript variable from different template. Commented Jul 26, 2016 at 11:50
  • This question doesn't make much sense. First of mySplitResult is being computed in a loop so it has multiple values. Secondly why are you joining strings only to split them again? If calender.calendario_slaves is an array then you can just use it. Thirdly, your helper doesn't return anything (there is a commented out return but its inside the while loop so that makes no sense either. What are you actually trying to do? Commented Jul 27, 2016 at 0:15

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.