骨干脚本不起作用

时间:2013-05-31 10:03:37

标签: backbone.js

我是Backbone Framework的新手,这是我的第一个应用程序。我在浏览器中看不到任何应用程序视图的渲染。我检查了错误控制台,但没有发现任何错误。你们看看并帮助我吗?我感谢您代表我的时间,并提前多多感谢

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
<script id="contactTemplate" type="text/template">
    <h1> <%= name %> </h1>
    <dl>
        <dt> <%= address %> </dt>
        <dt> <%= tel %> </dt>
        <dt> <%= email %> </dt>
        <dt> <%= type %> </dt>
    </dl>
</script>
<script>
  // Your code goes here
 (function ($) {

 /* Dummy JSON DataSet */   
 var contacts = [
                 {name:"Test1",address:"Test Address",tel:"0123",email:"test@test.com",type:"family"},
                 {name:"Test2",address:"Test Address",tel:"01234",email:"test@test.com",type:"friends"},
                 {name:"Test3",address:"Test Address",tel:"012345",email:"test@test.com",type:"office"}
                 ];

 /* Defining Model */ 
 var Contact = Backbone.Model.extend({
    defaults:{
        name:'',
        address:'',
        tel:'',
        email:'',
        type:''
    }
    });

 /* Defining Collection (Set of Models) */
 var Directory = Backbone.Collection.extend({
    model:Contact
    });

 /* View for rendering indivijual Model*/
 var ContactView = Backbone.View.extend({
    tagName:'div',
    className:'contact-container',
    template:$('#contactTemplate').html(),
    render:function(){
        var tmpl = _.template(this.template);        
        this.$el.html(tmpl(this.model.toJSON()));
        return this;
    }
    });

 /* View for rendering collection of Models */
 var DirectoryView = Backbone.View.extend({
    el:$("#contacts"),
    intialize:function(){        
        this.collection = new Directory(contacts);
        this.render();
    },
    render:function(){
        var that = this;
        _.each(this.collection.models, function(item){
            this.renderContact(item);
            },this);
    },
    renderContact:function(item){
        var contactView = new ContactView({
            model:item
        });        
        this.$el.append(contactView.render().el);
    }
    });

 /* Initializing the view */
 var directory = new DirectoryView(); 
} (jQuery));
</script>
</head>
<body>
<div id="contacts">    
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

当心,

 (function ($) {...})(jQuery)

不保证在DOM准备好时您的代码将被执行。在呈现DirectoryView时,<div id="contacts">尚不可用,因此$el也未定义。

将您的脚本放在</body>之后或放入document.ready将解决您的问题。

你这里也有一个错字:

    el:$("#contacts"),
    intialize: function(){    // <- initialize: function()    
        this.collection = new Directory(contacts);
        this.render();
    } 

答案 1 :(得分:0)

@ akoskm的回答是正确的,我改变了你的代码,如下所示:

ContactView = Backbone.View.extend({
    tagName: 'div',
    className: 'contact-container',
    template: $('#contactTemplate').html(),
    render: function(){
       var tmpl = _.template(this.template,this.model.toJSON());        
       this.$el.html(tmpl);
       return this;
   }
});


DirectoryView = Backbone.View.extend({
    el: "#contacts",
    initialize: function(options){        
        contacts = options.contacts
        this.collection = (new Directory(contacts)).models;
        this.render();
    },
    render: function(){
        var that = this;
        _.each(this.collection, function(item){
            this.renderContact(item);
            },this);
    },
    renderContact: function(item){
        var contactView = new ContactView({
            model: item
        });
        this.$el.append(contactView.render().el);
    }
});