Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用模板时主干生成错误_Javascript_Backbone.js - Fatal编程技术网

Javascript 使用模板时主干生成错误

Javascript 使用模板时主干生成错误,javascript,backbone.js,Javascript,Backbone.js,我是主干网的新手,我正在学习模板,但是我有这个输出错误 未捕获的TypeError:无法读取未定义下划线的属性“replace”。js:1305 _.template下划线.js:1305 (匿名函数) 这是我的index.js var Person = Backbone.Model.extend({ defaults: { name: 'ozan onder', age: 20, occupation: 'worker' } }

我是主干网的新手,我正在学习模板,但是我有这个输出错误 未捕获的TypeError:无法读取未定义下划线的属性“replace”。js:1305 _.template下划线.js:1305 (匿名函数)

这是我的index.js

var Person = Backbone.Model.extend({

    defaults: {
        name: 'ozan onder',
        age: 20,
        occupation: 'worker'
    }
});

var PersonView = Backbone.View.extend({
    //by default the tagname is a div element
    //but we will override it
    tagName: 'li',

    //% is how can access to a property name
    // create templer
    template: _.template($('#personTemplate').html()),

    initialize: function() {
        this.render();
    },

    render: function() {
        //set the html
        this.$el.html(this.template(this.model.toJSON()));
    }

});

var person = new Person;
var personView = new PersonView({ model: person });

console.log(personView.el);

And here is my index.html

<!DOCTYPE html>
<html> 
  <head> 
    <!--this import order is important-->
    <script src="../../library/jquery-1.8.2.min.js"></script>
    <script src="../../library/underscore.js"></script>
    <script src="../../library/backbone.js"></script>
    <script src="index.js"></script>

  </head>

  <body>

    <script id="personTemplate" type="text/template">
        <strong><%= name %></strong> (<%= age %>) - <%= occupation %>

    </script>

  </body>

</html>  
var Person=Backbone.Model.extend({
默认值:{
名称:“ozan onder”,
年龄:20,,
职业:“工人”
}
});
var PersonView=Backbone.View.extend({
//默认情况下,标记名是div元素
//但我们会推翻它
标记名:“li”,
//%是如何访问属性名的
//创建模板器
模板:35;.template($('#personTemplate').html()),
初始化:函数(){
这个。render();
},
render:function(){
//设置html
this.el.html(this.template(this.model.toJSON());
}
});
var人员=新人员;
var personView=newpersonview({model:person});
日志(personView.el);
这是我的index.html
()-

确保将代码包装在jQuery就绪函数中,以便在加载页面之前不会触发。也许您正在对代码初始化时不存在的DOM元素执行操作

$( document ).ready(function() {
   // put your code here
});

OR - They both accomplish the same thing.


$(function() {
  // put your code here
});

确保将代码包装在jqueryready func中,以便在加载页面之前不会触发。也许您正在处理一个在代码初始化时不存在的DOM元素。Thankss a lotttt:)@jamesemanon能否请您以答案的形式提供,以便它可以被接受?否则这个问题将永远排在“未回答的问题”队列中:-)好的,我现在就回答。谢谢