javascript - How to make reactive UI when variable is changed in Meteor? - Stack Overflow

I'm not sure that I understand how to keep my web application reactive with Meteor.I have this ver

I'm not sure that I understand how to keep my web application reactive with Meteor.

I have this very simple template

<body>
{{> simple}}
</body>

<template name="simple">
    Counter: {{counter}} <br/>
    <button>Increase</button>
</template>

And client side script

var counter = 0;

Template.simple.counter = function () {
    return counter;
}

Template.simple.events({
    'click button': function () {
        counter++;
        console.log("counter is ", counter);
        Meteor.flush();
    }
});

When clicking on button I can see in console that counter variable is increasing properly but nothing happens on UI. Why? I thought it's exactly what Meteor.flush() is intended to do.

I'm not sure that I understand how to keep my web application reactive with Meteor.

I have this very simple template

<body>
{{> simple}}
</body>

<template name="simple">
    Counter: {{counter}} <br/>
    <button>Increase</button>
</template>

And client side script

var counter = 0;

Template.simple.counter = function () {
    return counter;
}

Template.simple.events({
    'click button': function () {
        counter++;
        console.log("counter is ", counter);
        Meteor.flush();
    }
});

When clicking on button I can see in console that counter variable is increasing properly but nothing happens on UI. Why? I thought it's exactly what Meteor.flush() is intended to do.

Share Improve this question edited Aug 25, 2013 at 18:34 BenjaminRH 12.2k7 gold badges52 silver badges77 bronze badges asked Aug 25, 2013 at 18:06 Vitalii KorsakovVitalii Korsakov 47.7k21 gold badges75 silver badges92 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

The UI isn't reactive all by itself, you need to use one of Meteor's reactive items. In this case, you probably want to use Session. Try the following, instead of the second script you pasted:

Session.set('counter', 0); // Initialize a *reactive* variable

Template.simple.counter = function () {
    return Session.get('counter'); // This will automatically update, because Session is reactive
}

Template.simple.events({
    'click button': function () {
        Session.set('counter', Session.get('counter') + 1);
    }
});

You could also build your own reactive data source:

if (Meteor.isClient) {
  Sort = {
    sort: 1,

    dep: new Deps.Dependency,

    get: function () {
      this.dep.depend();
      return this.sort;
    },

    toggle: function () {
      this.sort *= -1;
      this.dep.changed();
    }
  };

  Template.leaderboard.players = function () {
    return Players.find({}, {sort: {score: Sort.get(), name: 1}});
  };

  Template.leaderboard.events({
    'click input.sort': function () {
      Sort.toggle();
    }
  });

}

Recent versions of Meteor provides the reactive-var package, see here : http://docs.meteor./#/full/reactivevar_pkg

To use ReactiveVar, add the reactive-var package to your project by running in your terminal:

meteor add reactive-var

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745533793a4631814.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信