- Router 代码
    var MyRouter = Backbone.Router.extend({ 
        routes: {
            // Other Routers
            "muppets/:id": "getMuppet"   
        },
        getMuppet: function (id) {                                                                                         
            console.log("fetch muppet: " + id);                                                                            
            var muppet = new MuppetModel({id: id});                                                                        
            muppet.fetch().then(function () {                                                                              
            console.log(muppet.get('name'));                                                                           
            var muppetView = new MuppetsListItemView({model: muppet, el: $('#muppets-list')});                                                                                     
            muppetView.render()                                                                                        
        });                                                                                                            
    }                                                                                                                  
});
- MuppetsListItemView代码
  var MuppetsListItemView = Backbone.View.extend({                                                                       
      tagName: 'li',                                                                                                       
      className: 'muppet',                                                                                                 
      template: _.template($('#muppet-item-tmpl').html()),                                                                 
      initialize: function() {                                                                                             
        this.listenTo(this.model, 'destroy', this.remove)                                                                  
      },                                                                                                                   
      render: function() {                                                                                                 
        var html = this.template(this.model.toJSON());                                                                     
        console.log(html, 'html of the itemView');                                                                         
        this.$el.html(html);                                                                                               
        return this;                                                                                                       
      },                                                                                                                   
      events: {                                                                                                            
        'click .remove': 'onRemove'                                                                                        
      },                                                                                                                   
      onRemove: function() {     
           this.model.destroy();                                                                                              
      }                                                                                                                    
});      - Template
    <script type="text/template" id="muppet-item-tmpl">                                                            
        <p><a href="/#muppets/<%= id %>" class=".showSingle"><%= name %></a></p>                                     
        <p>Job: <i><%= occupation %></i></p>                                                                         
        <button class="remove">x</button>                                                                            
    </script>  
                   
                   
                   
                   
                            
 
                             本文介绍了一个使用Backbone.js实现的简单应用案例,包括自定义路由器以响应URL变化,并通过模型获取数据更新视图的过程。展示了如何定义路由、获取参数、加载模型及渲染视图。
本文介绍了一个使用Backbone.js实现的简单应用案例,包括自定义路由器以响应URL变化,并通过模型获取数据更新视图的过程。展示了如何定义路由、获取参数、加载模型及渲染视图。
           
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   521
					521
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            