问题
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