2012-02-14 61 views
1

我正在使用Backbone Boilerplate構建一個應用程序,並且在獲取下劃線模板變量時遇到了一些麻煩。我有一個名爲Goal的資源。我的目標視圖的渲染功能如下:使用下劃線變量和主幹Boilerplate fetchTemplate函數

render: function(done) { 
    var view = this; 

    namespace.fetchTemplate(this.template, function(tmpl) { 
    view.el.innerHTML = tmpl(); 
    done(view.el); 
    }); 
} 

我打電話是另一種觀點的內部,像這樣:

var Goal = namespace.module("goal"); 

App.View = Backbone.View.extend({ 

    addGoal: function(done) { 

    var view = new Goal.Views.GoalList({model: Goal.Model}); 

    view.render(function(el) { 
     $('#goal-list').append(el); 
    }); 
    } 
}); 

我使用本地存儲來保存我的數據,它的存在添加得很好。我可以看到它在瀏覽器中,但由於某些原因,當我加載應用程序,並嘗試獲取現有的數據,我得到這個錯誤:

ReferenceError: Can't find variable: title 

在哪裏標題爲我存儲的唯一關鍵。這是直接呼叫的結果:

tmpl(); 

任何想法都非常感激。

+0

你的代碼中必須有一個變量'title'。是否沒有與錯誤消息關聯的行號? – Thilo 2012-02-14 03:57:10

回答

2

您的模板正在尋找一個變量title,可能是這樣的<%- title %>。你需要傳遞一個像這樣的對象tmpl({ title: 'Some title' })

0

原來,當我創建視圖時,我沒有傳入模型,這使得無法獲取模型數據。一旦我正確傳入模型,我就可以將數據傳遞給tmpl,正如@abraham所正確指出的那樣。

render: function(done) { 
    var 
    view = this, 
    data = this.model.toJSON(); 

    clam.fetchTemplate(this.template, function(tmpl) { 

    view.el.innerHTML = tmpl(data); 

    done(view.el); 
    }); 
},