English 中文(简体)
后骨+下划线 : 找不到模板变量 。 + 如何正确访问模型属性?
原标题:Backbone + Underscore: Can t find Template Variable. + Howto correctly access Modelattributes?

我刚从Backbone开始,我的目标是一个简单的幻灯片放映应用程序。

/ sliidecasts ( / sliidecasts )

给我一份所有可用的演示列表。 返回的json看起来像这个:

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

如果我想要访问单一演示文稿, 并获得第一张幻灯片,

slidecasts/1/slides

返回:

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

我要将演示文稿和幻灯片作为后骨模型。 我还想要有一个后骨视图, 其结尾仅显示幻灯片图像、 标题、 笔记和一个前向/ 后向按钮 。

这是我到目前为止的守则:

$(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 
      }
    });
}

对于收藏来说, 这对收藏有效, 但我真的不知道如何从收藏中获取单张幻灯片的信息 。 是否可以像幻灯片放映. slide ("0") 一样?

我对这段话也有些疑问:

我的 html 看起来像这个 :

<!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>

我总是得到一个错误, 即我想传给我的模板* 演示文稿_ name * 的变量名称没有定义, 尽管我已经定义了视图中的变量 。

最佳回答

模板范围内的变量必须明确通过。 模板不能自动访问视图范围中的变量。 而不是 :

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

尝试

var template = _.template( $("#slide_template").html(), variables);
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签