未捕获的TypeError:Object [object Object]没有使用backbone.js和coffeescript的方法'replace'

时间:2013-02-04 18:41:47

标签: javascript jquery backbone.js coffeescript

Person = Backbone.Model.extend(
  defaults: 
    name: 'Jony James'
    age: 30
    occupation:  'developer'

  validate: (attrs) ->
    if attrs.age < 0
      return 'Age must be positive, stupid.'
    if not attrs.name
      return 'A person must have a name! fool.'

  work: ->
    @get('name') + " is working." 
    )

PersonView = Backbone.View.extend({
    tagName: 'li'

    #template: _.template($('#personTemplate').html())
    template: "#personTemplate"

    initialize: ->
      @render()

    render: ->
      template = _.template($(@template))
      @$el.html(template)

    })

person = new Person
personView = new PersonView( model: person)
$(document.body).append personView.el

在我的index.html

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title></title>
 </head>
 <body>
  <script id="personTemplate" type="text/template">
   <strong><%= name %></strong> (<%= age %>) - <%= occupation %> 
  </script>
  <script src="js/underscore.js"></script>
  <script src="js/jquery.js"></script>
  <script src="js/backbone.js"></script>
  <script src="js/main.js"></script>
 </body>
</html>

template: _.template($('#personTemplate').html())@$el.html(@template(@model.toJSON()))工作正常。

但是使用当前版本的main.js我在Google Chrome控制台中收到此错误:

Uncaught TypeError: Object [object Object] has no method 'replace'

错误在哪里?

谢谢!

1 个答案:

答案 0 :(得分:2)

错误在模板功能中。尝试传递html而不是jQuery对象:

template = _.template($(@template).html(), @model.toJSON())