I have a few "user settings" that I want to be based on a query I do on start-up. For instance, all my setting fields are checkbox inputs and I'll set them to yes or no based on the preference they chose previously that we have stored in the database.
My query from the client looks as follows:
Meteor.call('getNotificationPreferences', function(err, result) {
console.log(result);
Session.set('aNotificationPreference', result.a);
Session.set('bNotificationPreference', result.b);
Session.set('cNotificationPreference', result.c);
Session.set('dNotificationPreference', result.d);
});
This call simply pulls that information from MongoDB and sets the session variable correctly, but then I want to use that session variable to set the checkbox field "checked" attribute. I've tried to set this directly with jQuery and also reactively with Meteor. Sometimes this works and sometimes it doesn't.
The HTML looks something like this (using jquery mobile):
<template name="preferences">
<div data-role="collapsible" data-collapsed="false" data-collapsed-icon="mail" data-expanded-icon="edit">
<h2>Notifications</h2>
<div class="ui-field-contain">
<label for="aNotificationSwitch">A</label>
<input type="checkbox" data-role="flipswitch" data-theme="b" name="aNotificationSwitch" id="aNotificationSwitch" data-mini="true" checked={{aNotification}}>
</div>
<div class="ui-field-contain">
<label for="bNotificationSwitch">B</label>
<input type="checkbox" data-role="flipswitch" data-theme="b" name="bNotificationSwitch" id="bNotificationSwitch" data-mini="true" checked="{{bNotification}}">
</div>
<div class="ui-field-contain">
<label for="cNotificationSwitch">C</label>
<input type="checkbox" data-role="flipswitch" data-theme="b" name="cNotificationSwitch" id="cNotificationSwitch" data-mini="true" checked="{{cNotification}}">
</div>
<div class="ui-field-contain">
<label for="dNotificationSwitch">D</label>
<input type="checkbox" data-role="flipswitch" data-theme="b" name="dNotificationSwitch" id="dNotificationSwitch" data-mini="true" checked="{{dNotification}}">
</div>
</div>
</template>
Then in my JavaScript, I have the following:
Template.preferences.aNotification = function() {
// Setting directly doesn't work either
//$('#aNotificationSwitch').prop('checked', Session.get("aNotificationPreference"));
return Session.get("aNotificationPreference");
};
Can anyone explain why this wouldn't work consistently?
I've also tried doing this the Meteor way, by publishing the preferences as follows:
Meteor.publish("user-preferences", function () {
return Meteor.users.find({_id: this.userId},
{fields: {'notification': 1}});
});