Retrieve nested collections and create DOM


#1

Hi everyone,

I’m quite uncertain how to write DOM from a very nested collection structure. so far my stuff was always just one level down but now my collections are more “complicated” and I don’t know how to set it up with the helpers and i’m also not sure if my strcuture/approach is even correct.

http://jsfiddle.net/avq201nc/4/

This is how it’ll look like approximately. I want the available controls to change based on database entry. When the user turns on one of the toggles on the right, I’ll activate a corresponding object in the collect (like business cards for example) and then the user can update the inidividual objects for the settings. Preferably I’d like to have the content retrieved from the collection, not hard coded in my templates.

		{{#each products_list}}

			{{userid}} - WORKS

			{{products}} - WORKS
			{{products.businesscards}} - WORKS
			{{products.businesscards.active}} - NOPE
			{{products.businesscards.dimensions[0]}} - NOPE


		{{/each}}

#2

this is achievable, i feel like i ran into something like this a while back…

does console.log(object) in your helpers give any insight? i seem to recall my issue was the JSON structure being a little off from what i thought it was…

do you have a repo we can clone and see whats going on?


#3

cough Don’t shoot me I’ve never used git. I think it’s time to learn it now ;).

I can go down in the console like so:

var foo = VendorProducts.find()

foo.fetch()[0].products.businesscards.dimensions[0].active

#4

in the fiddle its business-cards, which derailed me for a sec there :wink:

{{#each products.businesscards.dimensions}}
    {{active}}
    {{content}}
{{/each}}

Since dimensions is an array you can iterate over it with {{#each}} helpers…this (terribly mangled) meteorpad shows it using your JSON from the fiddle http://meteorpad.com/pad/Fo6xwLE3EADq6sapT/Leaderboard


#5

Ooops sorry about that. I changed things to business-cards because I think it doesn’t work in {{ }} helpers with a hyphen. Thanks, having a look at it now. Halfway through with the git tutorial :stuck_out_tongue:


#6

Following up

    <template name="player">
  <li class="player {{selected}}">
    <span class="name">
    {{#if products.businesscards.active}}
        <strong>ACTIVATE!!!</strong>
    {{else}}
        <small>awe...noactivate....</small>
    {{/if}}
    {{#each products.businesscards.dimensions}}
        {{active}}
        {{content}}
    {{/each}}
    </span>
    <span class="score">{{score}}</span>
 
  </li>
</template>

^ from the meteorpad (first time using meteorpad…pretty neat!)

and you are correct, handlebars borks with the hyphen


#7

This seems to be working quite nicely! Thanks, I’ve learned a great deal here! I didn’t even know that you don’t need to define the helper name to get it working lol.

Just wondering by the way, that’s something I’ve been wondering for a while but I’m not 100% sure. I’m wondering if it is super important that my database structure is correct. I’m not sure if this structure is considered ok but in case it’s not (and I won’t have more than a few dozen of those entries in the beginning per vendor), is it easy to convert things to other structures when you realize its not efficient/maintainable/whatever? Theoretically in such a case all you have to do is get all the collections, create a new collection out of that with the proper structure and then reference to your new collections, right?

Or is that total overkill on an app once you have an active userbase?


#8

Thoughtful db design is rarely a bad thing :wink:

Thankfully nosql is easier to change than sql solutions, and you can use a number of methods to refactor the documents after the fact (like splitting products into their own collections like business cards, posters, etc)…but like all things the job becomes more of a pain the larger the pile…


#9

Haha I’m sure it is. But I’m currently on the lookout for a technical co-founder. I am sure anyone taking over my code will have to re-factor lots of stuff. So wanna keep this in consideration what I should focus mostly on right now while I’m coding everything myself.

Anyway, you’ve helped me tremendously here, thanks a lot :).