erics15
erics15

Reputation: 627

Looping through array in Knockout and binding to HTML

I'm not so familiar with Knockout but learning it now as a crash course. I've been tasked with looping through this JSON object and putting in certain nodes into divs with the results. It's basically a video card display with certain bits of information to be displayed in each - type, info, thumbnail, and title. I'll then have to load a video player upon thumbnail click but that's later on. Any help would be appreciated for a newbie like me. It's just coming back undefined.

Here is my JSON Object. Am I going in the right direction here?

var json= {
    "available": true,
    "screens": [{
            "id": "s01",
            "type": "Selector",
            "config": {
                "views": [{
                    "id": "sintel",
                    "type": "selectorItem",
                    "thumbnail": "http://test-cdn.selectablemedia.com/test/a/sintel/assets/img/thumb_sintel.png",
                    "title": "Sintel",
                    "description": "Small video. HTML5, native controls, start poster. Bottom, right social. Video + grid",
                    "info": "00:51"
                }, {
                    "id": "bbb",
                    "type": "selectorItem",
                    "thumbnail": "http://test-cdn.selectablemedia.com/test/a/bbb/assets/img/thumb_bbb.png",
                    "title": "Big Buck Bunny",
                    "description": "Large video. HTML5, custom controls, social hover. Video + replay",
                    "info": "00:33"
                }, {
                    "id": "walle",
                    "type": "selectorItem",
                    "thumbnail": "http://test-cdn.selectablemedia.com/test/a/walle/assets/img/thumb_walle.png",
                    "title": "WALL-E",
                    "description": "Small video. Youtube, custom controls. Left social. Video + carousel.",
                    "info": "02:30"
               }]
            }
        },
        {
            "id": "sintel",
            "type": "BrandWrapper",
            "config": {
                "views": [{
                    "id": "sintelBranding",
                    "type": "fullBranding",
                    "url": "sintel_single.png"
                }, {
                    "id": "sintelPlayer",
                    "type": "smPlayerIframe",
                    "files": [{
                            "type": "mp4",
                            "codecs": "h264,aac",
                            "url": "http://test-cdn.selectablemedia.com/test/a/sintel/assets/video/sintel_trailer-1080p.mp4"
                        },
                        {
                            "type": "mp4",
                            "codecs": "avc1.42E01E,mp4a.40.2",
                            "url": "http://test-cdn.selectablemedia.com/test/a/sintel/assets/video/sintel_trailer-1080p.mp4"
                        },
                        {
                            "type": "ogv",
                            "codecs": "theora,vorbis",
                            "url": "http://test-cdn.selectablemedia.com/test/a/sintel/assets/video/sintel_trailer-1080p.ogv"
                        },
                        {
                            "type": "flv",
                            "codecs": "h264,aac",
                            "url": "http://test-cdn.selectablemedia.com/test/a/sintel/assets/video/sintel_trailer-1080p.flv"
                        }
                    ]
                }, {
                    "id": "sintelLeftClickArea",
                    "type": "brandClickArea",
                    "area": "leftTower",
                    "clickParams": {
                        "value": "https://durian.blender.org/"
                    }
                }, {
                    "id": "sintelHeaderClickArea",
                    "type": "brandClickArea",
                    "area": "header",
                    "clickParams": {
                        "value": "https://durian.blender.org/about/"
                    }
                }, {
                    "id": "sintelFooterClickArea",
                    "type": "brandClickArea",
                    "area": "footer",
                    "clickParams": {
                        "value": "https://durian.blender.org/gallery/"
                    }
                }]
            }
        },
        {
            "id": "bbb",
            "type": "BrandWrapper",
            "config": {
                "views": [{
                    "id": "bbbBrandingHeader",
                    "type": "header",
                    "url": "bbb_header.png"
                }, {
                    "id": "bbbBrandingLeft",
                    "type": "leftTower",
                    "url": "bbb_tower_left.png"
                }, {
                    "id": "bbbBrandingRight",
                    "type": "rightTower",
                    "url": "bbb_tower_right.png"
                }, {
                    "id": "bbbBrandingFooter",
                    "type": "footer",
                    "url": "bbb_footer.png"
                }, {
                    "id": "bbbPlayer",
                    "type": "smPlayerIframe",
                    "files": [{
                            "type": "mp4",
                            "codecs": "h264,aac",
                            "url": "http://test-cdn.selectablemedia.com/test/a/bbb/assets/video/bbb_trailer_1080p.mp4"
                        },
                        {
                            "type": "mp4",
                            "codecs": "avc1.42E01E,mp4a.40.2",
                            "url": "http://test-cdn.selectablemedia.com/test/a/bbb/assets/video/bbb_trailer_1080p.mp4"
                        },
                        {
                            "type": "ogv",
                            "codecs": "theora,vorbis",
                            "url": "http://test-cdn.selectablemedia.com/test/a/bbb/assets/video/bbb_trailer_1080p.ogv"
                        },
                        {
                            "type": "flv",
                            "codecs": "h264,aac",
                            "url": "http://test-cdn.selectablemedia.com/test/a/bbb/assets/video/bbb_trailer_1080p.flv"
                        }
                    ]
                }, {
                    "id": "bbbLeftClickArea",
                    "type": "brandClickArea",
                    "area": "leftTower",
                    "clickParams": {
                        "value": "https://peach.blender.org/"
                    }
                }, {
                    "id": "bbbHeaderClickArea",
                    "type": "brandClickArea",
                    "area": "header",
                    "clickParams": {
                        "value": "https://peach.blender.org/"
                    }
                }, {
                    "id": "bbbRightClickArea",
                    "type": "brandClickArea",
                    "area": "rightTower",
                    "clickParams": {
                        "value": "https://peach.blender.org/"
                    }
                }, {
                    "id": "bbbFooterClickArea",
                    "type": "brandClickArea",
                    "area": "footer",
                    "clickParams": {
                        "value": "https://peach.blender.org/"
                    }
                }]
            }
        },
        {
            "id": "walle",
            "type": "BrandWrapper",
            "config": {
                "assetPath": "http://test-cdn.selectablemedia.com/test/a/walle/assets/img/",
                "sizeTemplate": "smallVideo",
                "views": [{
                    "id": "walleBranding",
                    "type": "fullBranding",
                    "url": "walle_single.png"
                }, {
                    "id": "wallePlayer",
                    "type": "smPlayerIframe",
                    "files": [{
                            "type": "mp4",
                            "codecs": "h264,aac",
                            "url": "http://test-cdn.selectablemedia.com/test/a/walle/assets/video/wall-e-trailer-3_9_1280x544.mp4"
                        },
                        {
                            "type": "mp4",
                            "codecs": "avc1.42E01E,mp4a.40.2",
                            "url": "http://test-cdn.selectablemedia.com/test/a/walle/assets/video/wall-e-trailer-3_9_1280x544.mp4"
                        },
                        {
                            "type": "ogv",
                            "codecs": "theora,vorbis",
                            "url": "http://test-cdn.selectablemedia.com/test/a/walle/assets/video/wall-e-trailer-3_9_1280x544.ogv"
                        },
                        {
                            "type": "flv",
                            "codecs": "h264,aac",
                            "url": "http://test-cdn.selectablemedia.com/test/a/walle/assets/video/wall-e-trailer-3_9_1280x544.flv"
                        }
                    ]
                }, {
                    "id": "wallLeftClickArea",
                    "type": "brandClickArea",
                    "area": "leftTower",
                    "clickParams": {
                        "value": "http://movies.disney.com/wall-e/"
                    }
                }, {
                    "id": "walleHeaderClickArea",
                    "type": "brandClickArea",
                    "area": "header",
                    "clickParams": {
                        "value": "http://movies.disney.com/wall-e/"
                    }
                }, {
                    "id": "walleRightClickArea",
                    "type": "brandClickArea",
                    "area": "rightTower",
                    "clickParams": {
                        "value": "http://movies.disney.com/wall-e/"
                    }
                }, {
                    "id": "walleFooterClickArea",
                    "type": "brandClickArea",
                    "area": "footer",
                    "clickParams": {
                        "value": "http://movies.disney.com/wall-e/"
                    }
                }]
            }
        }
    ]
}

Below is some knockout code that I've tried to write but it's coming back undefined. I know it's something dumb :(. Just trying to log out so I can start doing this task

var PageViewModel = function (json) {
var self = this;
self.screens = ko.observableArray([]);
self.addSection = function (jsonobj) {
    console.log("jsonobj", jsonobj);
    self.screens.push(new my.VideoSection(jsonobj));
    console.log(self.screens());
}
for (var i = 0; i < json.screens.length; i++) {
    self.addSection(json.screens[i]);

}
};

return {
  pageViewModel: pageViewModel
}

My html to just start the loop

  <div data-bind="foreach: screens">
     <div class="container">
      <div class="row">    
        <div class="col-md-6 ">
            <div class="card">
                <div class="card-image">
                    <div  class="embed-responsive embed-responsive-16by9">

                </div>
                 </div><!-- card image -->

                <div class="card-content">
                    <span class="card-title" data-bind="text:title"> </span> 
                    <span class="card-title" data-bind="text:thumbnail"> </span>
                    <span class="card-title" data-bind="text:info"> </span>                      
                  </div><!-- card content -->
                </div>
            </div>
        </div>
      </div>

       <div class="row">    
        <div class="col-md-6 ">
            <div class="card">
               <div class="card-image">
                  <div  class="embed-responsive embed-responsive-16by9">

                </div>
                </div><!-- card image -->

                <div class="card-content">
                    <span class="card-title" data-bind="text:title"> </span> 
                    <span class="card-title" data-bind="text:thumbnail"> </span>  
                    <span class="card-title" data-bind="text:info"> </span> 

                 </div><!-- card content -->
              </div>
            </div>
        </div>
      </div>
    </div>

Upvotes: 1

Views: 44

Answers (1)

Brother Woodrow
Brother Woodrow

Reputation: 6382

You seem to be on the right track. Couple of things to note:

  • You didn't post the code for the my.VideoSection constructor;
  • You're looping through screens correctly, but a screen object has a config propery that has a views array that you also need to loop through. You could either do foreach: config.views or switch the binding context to the config property on the parent element using the with binding.

So basically, this:

<div data-bind="foreach: screens">
    <div class="container" data-bind="with: config">
        <div class="row" data-bind="foreach: views">    
            <div class="col-md-6">
                <div class="card">
                    <img class="card-img-top" data-bind="attr: { src: thumbnail }">
                    <div class="card-body">
                        <h5 class="card-title" data-bind="text:title"> </h5> 
                        <p class="card-text" data-bind="text:info"> </p>                      
                    </div><!-- card content -->
                </div>
            </div>
        </div>
    </div>
</div>

Fiddle: https://jsfiddle.net/thebluenile/9yvpfr7n/

Upvotes: 2

Related Questions