Global array/object in helpers?


#1
Template.hello.helpers({
    niceArray: [
    {
      index: 1,
      title: 'hello',
      price: '5-20',
      chance: Session.get('something')
    }
    ]
  });


  Template.hello.events({
    'click .doit': function (e) {
      var blabla = niceArray[(e.target.dataset.id) - 1];

Okey. so im trying to grab niceArray in hello.events. But it seems like its not global.

If i create the array (not sure if it is called array or object) outside the helpers. It becomes global and i can use it anywhere. But the problem with this is that it doesnt get updated to the client if i change something in it.

How can i solve this problem?


#2

put it in the settings.json under the public key


#3

Do i create a settings.json file? I guess

What is public key?


#4

Just do it this way:

var niceArray = [ .... values ... ];

Template.hello.events({
  'click .doit': function (e) {
    var blabla = niceArray[(e.target.dataset.id) - 1];

I don’t think that’s valid syntax for Meteor helpers. Helpers are expected to be functions that return something to be used in a template via {{helperName}}.


#5

Yeah, that works. But if i change something in the niceArray. It doesnt get updated directly to the client. How can i solve this?


#6

Make niceArray reactive, like so:

Session.set('myNameSpace/niceArray', [
  {
    index: 1,
    title: 'hello',
    price: '5-20',
    chance: Session.get('something')
  },
  { ... }
]);

If you’re going to use Session, introduce some sort of namespacing or grouping in your variable names to keep things clean. Session.set('group/specificName', ...).


#7

PS: This feels like niceArray should be a MongoDB collection.


#8

Thank you a lot for your help. I appreciate it!

Im not going to add more stuff to the array, just change a variable slightly. So i think this is enough for now. But ill think about it as i continue developing


#9

No problem!

Also keep in mind, if you don’t want to store temporary data such as this in MongoDB, you can use a temporary unmanaged collection:

NiceArray = new Mongo.Collection(null);

This collection not stored on the server, and is discarded as soon as the client leaves the website.


#10
Session.set('coolNamespace/pikachu', [
    {
      index: 1,
      title: 'Narnia',
      price: '5-20',
      chance: (60 * Session.get('thisDoesntUpdate'))
    }]);


Template.hello.helpers({
    thisPrintsToTheClient: Session.get('coolNamespace/pikachu')
  });


  Template.hello.events({
    'click .doit': function (e) {

      var stuff = Session.get('coolNamespace/pikachu')[(e.target.dataset.id) - 1];

It doesnt seem to update directly to the site still :confused:

{{#each thisPrintsToTheClient}}
    <div class="popcorn">
      <div data-id="{{index}}" class="doit">Well</div>
      <div class="chans">{{chance}}%</div>
      <div class="valuta">{{price}}kr</div>
      <div class="title">{{title}}</div>
    </div>
  {{/each}}

#11

Helpers should return functions.

Template.hello.helpers({
  thisPrintsToTheClient: function () {
    return Session.get('coolNamespace/pikachu')
  }
});

#12

Hmm, it still doesnt update directly.

It does update if i make the server restart (by making a small change and saving)

But it doesnt change instantly, without a server-restart :<


#13

Try this:

http://meteorpad.com/pad/Zt8cgrR3k8hWNDWDd/pikachu


#14
Template.hello.events({
    'click .doit': function (e) {

      var stuff = Session.get('coolNamespace/pikachu')[(e.target.dataset.id)
      var chance = stuff.chance;

I’m using this though to grab the chance from the object to hello.events. I dont think that solution is possible?

I’m new at meteor so im sorry if i am slow

EDIT:

I got it working! ^^

var chance = (stuff.chanceMultiplier) * Session.get(‘level’);


#15

Good! No need to apologize, there’s a lot to learn about Meteor. I’m still learning daily. :smile:


#16

You can use ReactiveArray package for that too, and you will declare it as var.