Nicolas
Nicolas

Reputation: 1848

Backbone + Underscore: Can't find Template Variable. + Howto correctly access Modelattributes?

I just started with Backbone and my goal is a simple slidecasting application. I have a REST Interface where

/slidecasts

gets me a list of all available presentations. the returned json looks like this:

[
    {
        "name": "p75-barth.pdf", 
        "nr": 1, 
        "slideLinks": [
            "slides/0", 
            "slides/1",                
        ]
}]

If I want to access a single presentation and get for example the first slide I can simply do:

slidecasts/1/slides

which returns:

[
    {
        "content": null, 
        "imageLinks": [
            "images/1"
        ], 
        "nr": 0, 
        "title": null
    }
]

I want to represent a Presentation and Slides as Backbone Models. I also want to have a Backbone View which in the end simply shows a slide-image, the title, some notes and a forward/backward button.

this is my code so far:

$(function (){

    initModel();
    initCollection();
    initView();

    var testSlidecast = new Slidecast();
    testSlidecast.id=1;

    testSlide = new Slide();
    testSlide.id = 5;

    testSlidecast.Slide = testSlide;

    var slideView = new SlideView(({model: testSlidecast}));

})

function initView(){
    SlideView = Backbone.View.extend({
        el: $("#presentation"),
        initialize: function(){
            this.render();
            alert("asd")
        },
        render: function(){
            var variables = { presentation_name: "This is a Test-Slide-Name" };
            var template = _.template( $("#slide_template").html(), {} );
            this.el.html( template );
        }
    });
}

function initModel(){
    Slide = Backbone.Model.extend({
    });
}

function initCollection(){
    Slidecast = Backbone.Collection.extend({
      model: Slide,
      url: function(){
        return 'slidecasts/' + this.id + '/slides'
      }
    });
}

This works for a Collection but I don't really know how I can get the Infos for a single Slide from there. Is something like slidecast.slide("0") possible?

I also have some problems regarding the templating for my view:

My html looks like this:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <link href="style.css" rel="stylesheet"/>

    <script src="http://underscorejs.org/underscore-min.js" > </script>
    <script src="http://code.jquery.com/jquery-1.7.2.min.js" > </script>
    <script src="http://backbonejs.org/backbone-min.js" > </script>
    <script src="bscript.js" > </script>

    </head>
    <body>
        <div id="presentation"> </div>

    <script type="text/template" id="slide_template">
            <label>Presentation <%= presentation_name %>  </label>
            <img src="" id="slide_pic" />
            <input type="text" id="slide_content" value="asd" />
    </script>

    </body>

</html>

I keep getting the error that the variable name i want to pass to my template *presentation_name* is not defined although I have defined the variable in the view.

Upvotes: 1

Views: 2596

Answers (1)

Peter Lyons
Peter Lyons

Reputation: 146014

The variables in the scope of the template must be explicitly passed. The template does not get access to variables in the view's scope automatically. Instead of:

var template = _.template( $("#slide_template").html(), {} );

Try

var template = _.template( $("#slide_template").html(), variables);

Upvotes: 3

Related Questions