这就是我目前的做法,但我想做的是从模板中渲染数据而不这样做(serialized_model.items [0]):
template: function(serialized_model){
var user_data_tmpl = $("#user-data-template").html();
var template = Handlebars.compile(user_data_tmpl);
return template(serialized_model.items[0]);
}
我很难得到结果,但这是完整的代码:
模型与收藏:
window.UserData = {};
UserData.Model = Backbone.Model.extend();
UserData.Collection = Backbone.Collection.extend({
model:UserData.Model,
url: '/getUserData',
parse: function(response) {
return response.data;
}
});
风景:
UserData.View = Backbone.Marionette.ItemView.extend({
template: function(serialized_model){
var user_data_tmpl = $("#user-data-template").html();
var template = Handlebars.compile(user_data_tmpl);
return template(serialized_model.items[0]);
}
});
模板:
<script id='user-data-template' type='text/x-handlebars-template'>
<a href="#" id="logout"> Logout </a>
uid:{{uid}} username:{{username}} clave:{{clave}}
</script>
JSON:
{"success":true,"data":{"logged":true,"uid":14,"clave":14,"depto":9,"nivel":5},"report":null}