簡體   English   中英

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

[英]Uncaught TypeError: Object [object Object] has no method 'replace' with backbone.js and 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我在谷歌Chrome控制台中收到此錯誤:

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

錯誤在哪里?

謝謝!

錯誤在模板功能中。 嘗試傳遞html而不是jQuery對象:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM