【问题标题】:Meteor subscription won't set checkbox if app is refreshed如果刷新应用程序,Meteor 订阅将不会设置复选框
【发布时间】:2014-11-19 08:03:38
【问题描述】:

我遇到了一个问题,Meteor 订阅在刷新后未将复选框设置为“选中”。基本上,如果我运行 Meteor 并更改 JS,应用程序将按预期工作(从 Meteor.user() 提取数据并相应地设置复选框)。但是,如果我刷新应用程序,我的所有复选框都设置为 false。我不明白为什么会发生这种情况,有什么想法吗?

我的订阅如下:

服务器端

Meteor.publish("user-preferences", function () {
    if (this.userId) {
        return Meteor.users.find({_id: this.userId},
            {fields: {'notification': 1}});
    } else {
        this.ready();
    }
});

客户端:

Meteor.subscribe("user-preferences", function() {
    if (Meteor.user()) {
        var notification = Meteor.user().notification;
        // Check to see if notification and systems exists; if not, default to TRUE
        if (notification) {
            Session.set('aNotificationPreference', notification.hasOwnProperty('a') ? notification.a : true);
        }
        else {
            Session.set('aNotificationPreference', true);
        }
    }
    else {
        // TRUE too if no user yet
        Session.set('aNotificationPreference', true);
    }
});

这是查找会话变量以使事情反应的助手:

Template.preferences.helpers({
    isChecked: function() {
        console.log('#### PREF INITIAL: ' + Session.get("aNotificationPreference"));
        var pref = Session.get("aNotificationPreference");
        if (typeof pref === 'undefined') {
            Meteor.setTimeout(function() {
                pref = Session.get("aNotificationPreference");
                console.log('#### PREF IN TIMEOUT: ' + pref);
                return pref ? 'checked' : false;
            }, 1250);
        }
        else {
            console.log('#### PREF in ELSE: ' + pref);
            return pref ? 'checked' : false;
        }
    }
});

最后,这是 HTML 复选框字段:

<input type="checkbox" data-role="flipswitch" data-theme="b" name="aNotificationSwitch" id="aNotificationSwitch" data-mini="true" checked={{isChecked}}>

这是基于 Blaze documentation 在此专门和我发现的其他帖子上的。

我知道问题出在帮助程序中,但我不确定如何解决。失败日志如下:

Application Cache Checking event (index):1
Application Cache NoUpdate event (index):1
#### PREF INITIAL: undefined index.js?8b2a648142fb63b940f4fb04771d18f25b5bf173:63
Connected to Meteor Server. index.js?8b2a648142fb63b940f4fb04771d18f25b5bf173:37
#### PREF INITIAL: true index.js?8b2a648142fb63b940f4fb04771d18f25b5bf173:63
#### PREF in ELSE: true index.js?8b2a648142fb63b940f4fb04771d18f25b5bf173:73
Connected to Meteor Server. index.js?8b2a648142fb63b940f4fb04771d18f25b5bf173:37
#### PREF IN TIMEOUT: true 

【问题讨论】:

  • 似乎有很多地方出了问题。您是否一路添加了一些console.log 语句,例如,在subscribeonReady 函数中?你检查过控制台中会话变量的值吗?
  • 是的,就获取值而言,一切正常,但我看到的是当我看到这个失败时,助手中的 Session 变量是未定义的,所以我猜这和订阅存在竞争条件.在设置模板之前,如何强制助手等到设置该值?此外,为什么在设置会话变量后它不是响应式的,因为一旦订阅返回它就会被设置。

标签: meteor


【解决方案1】:

我还不是 Meteor 方面的专家。

但我会更改您的 JavaScript:

if(Meteor.isClient){

  Template.preferences.helpers({
    isChecked: function() { return Meteor.user().notification ? 'checked' : false; }
  });

  Template.preferences.events({
    'click #aNotificationSwitch': function(event){
      var checked = event.target.checked; // true or false
      Meteor.users.update({_id: Meteor.userId()}, $set: {notification: checked});
    }
  });
}

不过,这与您的方法不同。通过这种方式,您不需要使用会话变量。

允许更新用户:

Meteor.users.allow({
  update: function(userId, user){
    return user._id == userId;
  }
});

【讨论】:

  • 仍然遇到同样的问题。您必须在 Meteor.user() 调用之外为您的通知对象进行订阅,因此如果尚未填写订阅,则在呈现模板时该评估将是未定义的。似乎您无法在渲染后更改复选框 checked attr 的值,因为从我的日志中可以看到,助手再次被调用并且值为 TRUE
  • 我忘了添加你必须允许用户更新,你知道怎么做吗?似乎允许超过 pub/sub 的问题
  • 很抱歉,这与问题有什么关系?我在更新用户记录时没有任何问题。效果很好!问题正如我在之前的评论中所说的那样。
  • 对不起,我不明白你的第一条评论编辑
【解决方案2】:

我发现的最佳解决方案是通过在Template.body.events 中的面板打开事件上执行以下操作来使用 jQueryMobile 设置翻转开关:

'panelbeforeopen #mypanel': function() {
    //Refresh flipswitches prior to opening sidepanel otherwise they default to disabled
    $("#aNotificationSwitch").flipswitch("enable").flipswitch("refresh");
    $("#bNotificationSwitch").flipswitch("enable").flipswitch("refresh");
    $("#cNotificationSwitch").flipswitch("enable").flipswitch("refresh");

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 2014-03-04
    • 2020-11-13
    • 1970-01-01
    • 2016-01-16
    • 2014-12-11
    相关资源
    最近更新 更多