0

I'm trying to get my head around the way meteor work. I'm building a site prototype and I'd like to pre-populate it with data like site.title, site.logo, site.contact.number etc...

I've created a Settings collection:

Settings = new Meteor.Collection('settings');
Settings.insert(
  {
  logo: 'test logo',
  contact: {
    human: '01 01 01 01 01',
    machine: '0101010101'
  }
}
)

is it possible in the html markup to then retrieve this data across templates, as Meteor is subscribed to the collection?

I'm trying to do things like:

    <a href="#" class="logo url" rel="me">{{settings.logo}}</a>
    <a href="{{settings.contact.machine}}" class="logo url" rel="me" {{settings.contact.human}}</a>

Meteor is running on auto publish at the moment so I'm not sure if I need to do something in my main.js file. Can Meteor access all values automatically? at the moment it prints [object,object]


update

I've created a settings.json file:

{
  "public" : {
    "logo" : "my fancy company name",
    "contact" : {
      "human" : "01 01 01 01 01 ",
      "machine" : "0044101010101"
    }
  }
}

Then I changed my Handlebars.registerHelper to

Handlebars.registerHelper('view', function(){
  return Meteor.settings.public
});

I can now access all of the settings without creating a Collection for them, much more easily.
cheers

Yannick Schall
  • 32,601
  • 6
  • 29
  • 42

1 Answers1

1

Your approach is wrong on several levels.

The handlebar expression {{settings.contact.machine}} will insert *currentContextObject*.settings.contact.machine into the HTML, where *currentContextObject* is the template's data context.

What is your template's data context? I don't know but it doesn't matter because settings.contact.machine won't make sense either way. Settings is a collection of documents but you are trying to use it as if it were a single object.

What would work in JS is Settings.findOne().contact.machine. To access this setting across templates you would need to create a global template helper like e.g.:

if (Meteor.isClient) {
  Handlebars.registerHelper("getMachineContact", function() {
    return Settings.findOne().contact.machine;
  });
} 

Then you could use {{getMachineContact}} in your HTML.

Still this solution wouldn't be nice and you should probably be using Meteor.settings instead of a Settings collection for solving your use case.

Similar to here you could then create a global template helper that returns arbitrary values from Meteor.settings given their path, meaning you could for example write {{getSetting "contact.machine"}}. This approach would involve converting a string in dot notation ("contact.machine") into an object reference so this question might be useful.

Community
  • 1
  • 1
Tobias
  • 4,034
  • 1
  • 28
  • 35
  • I'm trying to do something that work a bit like a .yaml config file in jekyll. I want to set up key values to fill in the markup when I prototype. I'm looking for something basic. I actually was in my own way "almost" there. I add a helper with `return Settings.find()`. So thanks for the `Settings.findOne().blah.blah` tip :). I'm not that big on javascript so it's quite a handful of new concept here. – Yannick Schall Jan 15 '14 at 16:34
  • But just to make sure, you're saying that I don't need to create a Settings collection as I can do the same in Meteor.settings? – Yannick Schall Jan 15 '14 at 16:41
  • Yes I can. Thanks very much for the enlightenment :) – Yannick Schall Jan 15 '14 at 17:39