2012-09-13 55 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

更新了网址 –

相关问题