2014-01-21 43 views
0

我在骨幹js中練習使用外部模板,我得到了這個TypeError,它說對象沒有templateBackbone js丟失方法'模板'

此外路由器如何識別/調用視圖,如果它在另一個路徑中,反之亦然?

我已經提供了我工作的代碼:

profile.js

window.ProfileView = Backbone.View.extend({ 

initialize: function() { 
    this.render(); 
}, 

render: function() { 
    $(this.el).html(this.template()); 
    return this; 
} 
}); 

main.js

var AppRouter = Backbone.Router.extend({ 
    routes: { 
     'profile' : 'profile' 
    }, 
    profile: function() { 
     this.profileView = new ProfileView(); 
     $('#global-content').html(this.profileView.el); 
    } 
}); 


utils.loadTpl (['profile'], function() { 
    appRouter = new AppRouter(); 
    Backbone.history.start(); 

}); 

utils.js

window.utils = { 
loadTpl: function(views, callback) { 

    var deferreds = []; 

    $.each(views, function(index, view) { 
     if (window[view]) { 
      deferreds.push($.get('templates/' + view + '.html', function(data) { 
       window[view].prototype.template = _.template(data); 
      })); 
     } else { 
      // alert(view + " not found"); 
     } 
    }); 

    $.when.apply(null, deferreds).done(callback); 
} 
}; 

回答

1

模板不是一個函數,你需要調用它作爲視圖的一個屬性,因此您應該刪除()

$(this.el).html(this.template); 
+0

對,它擺脫了錯誤,但模板仍然不顯示 – OneScrewLoose

+1

你是如何設置模板,這是不明顯在你的代碼中,你是。通常我使用require.js來檢索資源,然後專門設置模板,例如'template:template,'在視圖本身 –

+0

它是在utils.js中指定的。我故意沒有使用require.js 引用[this](https://github.com/ccoenraets/nodecellar/tree/master/public) – OneScrewLoose