backbone.js: accessing a property on deeplinked page

元气小坏坏 提交于 2020-01-16 11:22:08

问题


I am working on an example using a router to switch to a new view. In this example the view's render function is using a console.log to successfully outputs the attributes, including the id, but the template does not display the id. I have tried to only put the relevant sections of the application here.

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)); 

});

来源:https://stackoverflow.com/questions/15225549/backbone-js-accessing-a-property-on-deeplinked-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!