1

我正在尝试运行此代码,但出现此错误:Uncaught TypeError: string is not a function在哪里写:var html = template(context); 有人知道原因吗?

看法

define(["jQuery", "underscore", "Backbone", "Handlebars","models/person" ,"text!templates/userlistview.html"], 
function($, _, Backbone, Handlebars,Person, template) {

    var UserListView = Backbone.View.extend({

        template: Handlebars.compile(template),

        render: function() {
          var context = JSON.parse(JSON.stringify(this.model));
          console.log(context);
          var html = template(context);
          $(this.el).html(html);

          return this;
        }

     });

     return UserListView;
});

路由器

define(["jQuery", "underscore", "Backbone", "collections/usercollection", "models/person", "views/userlistview"], 
function($, _, Backbone, Usercollection, Person, UserListView) {

    var AppRouter = Backbone.Router.extend({

        routes: {
            "": "list",
        },

        list: function() {
            this.utenti= new Person({name:"stefano",cognome:"magli"});
            this.page= new UserListView({model:this.utenti});

            this.page.render();
        }

    });

    return AppRouter;

});
4

1 回答 1

2

添加this

var html = this.template(context);

代替:

var html = template(context);

templateis "text!templates/userlistview.html", whilethis.template是你编译的模板。

编辑:

这与您的问题无关,但您可以替换JSON.parse(JSON.stringify(this.model))this.model.toJSON().

于 2012-07-04T13:14:12.830 回答