andresmijares
andresmijares

Reputation: 3744

passing model´s array to an underscore view

I'm trying to pass some models attibutes to my underscore view, however, for some reason I cannot find the right way to make it work, if some1 can point in the right direction, I would appreciated it.

 App.appModel = new App.Models.AppModel({
    "user" : data.user,
    "acls" : data.acls //acls is an array, which I need to pass to the view
 });
 App.appLogged = new App.Views.App({
    model : App.appModel
 });


 //My view
 App.Views.App = Backbone.View.extend({
     render : function() {
       template = _.template( $('#Home').html(), {acls : this.model.toJSON }) ;
       this.$el.html(template);
     }
 });


//so In my view... I need a li with each acl
<script type="text/template" id="Home">
      <% _.each(acls, function(acl) { %>
        <li><%= acl.get(0)  %></li>
    <% }); %> 
</script>

It doens't throw any error... it just dont render it...

Thanks in advance.

Upvotes: 0

Views: 73

Answers (2)

CharlieBrown
CharlieBrown

Reputation: 4163

Change your template compilation line:

//My view
 App.Views.App = Backbone.View.extend({
     render : function() {
       template = _.template( $('#Home').html(), this.model.toJSON()) ;
       this.$el.html(template);
     }
 });

model.toJSON will produce an object with keys corresponding to the model attributes. In this case, it will already contain the key acls. What you were producing is

{ acls: { acls: [], ... } }

And what your template needs is:

{ acls: [xxxx] }

Normally it's useful to make a call to console.log(this.model.toJSON()) on your render, to see what's going into your template.

Upvotes: 1

Myk Willis
Myk Willis

Reputation: 12879

Looks like missing parens in the call toJSON()?

 //My view
 App.Views.App = Backbone.View.extend({
     render : function() {
       template = _.template( $('#Home').html(), {acls : this.model.toJSON() }) ;
       this.$el.html(template);
     }
 });

Upvotes: 1

Related Questions