1

我是 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>
4

2 回答 2

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();
    } 
于 2013-05-31T10:44:51.920 回答
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);
    }
});
于 2013-05-31T12:33:01.413 回答