rendrjs / rendr

Render your Backbone.js apps on the client and the server, using Node.js.
MIT License
4.09k stars 313 forks source link

how to pass model to subview #502

Closed jaredrada closed 8 years ago

jaredrada commented 8 years ago

This is a question but figured Github is more heavily trafficked than Stack Overflow for this topic. 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!

crwang commented 8 years ago

Hi @jaredrada ,

Please post this on SO and link to it here. We're trying to push these sorts of questions more onto SO and to have Github be more with issues around the framework itself.

Thanks!

jaredrada commented 8 years ago

Thanks @crwang, thats understandable. I posted the question on SO... http://stackoverflow.com/questions/33463139/rendr-how-to-pass-model-to-subview-in-rendrjs-isomorphic-framework

Feel free to help answer since the SO traffic related to RendrJS is minimal!

crwang commented 8 years ago

Yeah, very minimal, that's why we're trying to push more that way! :smile:

crwang commented 8 years ago

Cool, I also posted an answer. Would you mind closing out this Github issue too? Thanks!