2012-09-13 64 views
0

我是backbone.js和handlebars的新手,我有一個問題讓我的模板渲染出數據。backbone.js,handlebars錯誤:this._input.match不是函數

這裏是tagfeed.js模塊我的採集和模型數據:

// Create a new module. 
    var Tagfeed = app.module(); 

    // Default model. 
    Tagfeed.Model = Backbone.Model.extend({ 
    defaults : { 
     name : '', 
     image : '' 
    } 
    }); 

    // Default collection. 
    Tagfeed.Collection = Backbone.Collection.extend({ 
    model : Tagfeed.Model, 
    url : Api_get('api/call') 
    }); 

    Tagfeed.TagView = Backbone.LayoutView.extend({ 
    template: "tagfeed/feed", 
    initialize: function() { 
     this.model.bind("change", this.render, this); 
    }, 
    render: function(template, context) { 
       return Handlebars.compile(template)(context); 
    } 
    }); 

然後在我的路由器,我有:

define([ 
    // Application. 
    "app", 

    // Attach some modules 
    "modules/tagfeed" 
], 

function(app, Tagfeed) { 

    // Defining the application router, you can attach sub routers here. 
    var Router = Backbone.Router.extend({ 

    routes: { 
     "index.html": "index" 
    }, 

    index: function() { 
     var collection = new Tagfeed.Collection(); 

     app.useLayout('main', { 
      views: { 
       ".feed": new Tagfeed.TagView({ 
        collection: collection, 
        model: Tagfeed.Model, 
        render: function(template, context) { 
         return Handlebars.compile(template)(context); 
        } 
       }) 
     } 
    }); 
    } 
    }); 

    return Router; 

}); 

這個成功使得該API的調用,進行呼叫獲取我的主模板,並調用獲取Feed模板的HTML。如果我沒有包含該渲染(模板,上下文)功能,那麼它會在頁面上呈現爲我在包含{{name}}的Feed模板中包含的直線HTML。但其包含的時候,我得到的錯誤

TypeError: this._input.match is not a function 
[Break On This Error] 

match = this._input.match(this.rules[rules[i]]); 

,如果我檢查該得到傳遞到appLayout意見渲染功能feed變量,我看到template var是一個功能,並且context VAR是未定義,那麼它會拋出該錯誤。

任何想法我做錯了什麼?我知道我在這裏至少有一個問題,可能更多。

回答

1

由於您使用的是requirejs,因此您可以使用文本模塊來外化您的模板,或者更好地對它們進行預編譯並將它們包含在視圖中。退房http://berzniz.com/post/24743062344/handling-handlebars-js-like-a-pro

E.g.使用預編譯模板

// router.js 
define(['views/tag_feed', 'templates/feed'], function(TagFeedView) { 

    var AppRouter = Backbone.Router.extend({ 

     // ... 

    }); 

}) 

// tag_feed.js 
define(['collections/tag_feed'], function() { 

    return Backbone.View.extend({ 

     // ... 

     render: function() { 
      this.$el.html(
       Handlebars.templates.feed({ 
        name: '...' 
       }) 
      ); 
     } 

    }); 

}) 

僅供參考我創建簡單的樣板爲骨幹/需要你/車把安裝https://github.com/nec286/backbone-requirejs-handlebars

+0

鏈接樣板例子是404 :( – andyzinsser

+0

更新了網址 –

相關問題