Stefano Maglione
Stefano Maglione

Reputation: 4150

backbone.js and handlebars:string is not a function

I'm trying to run this code but this error appear: Uncaught TypeError: string is not a function at line where is written : var html = template(context); Does anybody know the reason?

view

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;
});

router

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;

});

Upvotes: 1

Views: 1861

Answers (1)

CD..
CD..

Reputation: 74096

add this:

var html = this.template(context);

instead of:

var html = template(context);

template is "text!templates/userlistview.html", while this.template is your compiled template.

EDIT:

this has nothing to do with your question, but you can replace JSON.parse(JSON.stringify(this.model)) with this.model.toJSON().

Upvotes: 2

Related Questions