backbone.js:访问深度链接页面上的属性

时间:2013-03-05 13:54:10

标签: backbone.js deep-linking backbone-routing

我正在研究一个使用路由器切换到新视图的示例。在此示例中,视图的render函数使用console.log成功输出属性,包括id,但模板不显示id。我试图只将应用程序的相关部分放在这里。

var DetailView = Backbone.View.extend({
 el: $('body'),
 template: _.template('this is detailview for <% id %><a href="index.htm">back</a>'),

 render: function(id){
     var attributes = this.model.toJSON();
     this.$el.append(this.template(attributes));
     console.log(attributes);
 }
});

  var AppRouter = Backbone.Router.extend({
    routes: {
       "posts/:id": "getPost",
        "*actions": "defaultRoute" // matches http://example.com/#anything-here
    }
});

var app_router = new AppRouter;

app_router.on('route:getPost', function (id) {

    var thing = new Thing({ title:'first thing', id:'3' });
    detailView = new DetailView({model:thing}); 
    detailView.$el.empty();
    detailView.$el.append(detailView.render(id)); 

});

0 个答案:

没有答案