Search code examples
javascriptrendr

Rendr how to pass model to subview in RendrJS isomorphic framework


Assume I'm building a blog where the home page renders a list of blog posts. My question is how to pass a model to each child post view?

I have an index.hbs iterating through a collection

{{#forEach models}}
    {{view "post_view" model=value model_name="story"}}
{{/forEach}}

I have a post_view.js

var BaseView = require('./base');

module.exports = BaseView.extend({
     className: 'post_view',

    initialize: function() {
        console.log(this.model); //undefined
        console.log(this.options.model); //undefined on client, logs model attributes on server
    }
});

module.exports.id = 'post_view';

But it appears the model isn't set inside the post_view.js. If I do a {{json value}} inside the {{forEach}} loop I can see the JSONified output printed. What do I need to do to pass a model, do I need to manually construct it inside the view? Thanks!


Solution

  • The answer ended up being the improper use of the parse() method. My original stories collection had a property like this

    parse: function(rsp) {
        return rsp.stories;
    }
    

    Because the API response contained some metadata as well as an array of stories under the "stories" key. I removed that parse method and instead added

     jsonKey: 'stories'
    

    Which resolved my problem.