我在与来自两个数组的 ID 建立链接时遇到问题...
表格的此视图(模板)....单击到 li 事件后不起作用...请帮助我。
<script type="text/template" id="table-template">
<% _.each(tables, function(table) { %>
<% _.each(table.get("tables"), function(table) { %>
<li class="tableli" data-table_id="<%= table.id %>">
<div class="obrazok"></div>
<%= table.name %>
</li>
<% }); %>
<% }); %>
</script>
有菜单模板:
<script type="text/template" id="menu-template">
<% _.each(menus, function(menu) { %>
<% _.each(menu.get("menus"), function(menu) { %>
<li class="menucls" data-menu_id="<%= menu.id %>">
<%= menu.name %>
</li>
<% }); %>
<% }); %>
</script>
我的收藏...
//====
//! Tables
//====
var Tables = Backbone.Collection.extend({
url: 'api/menus_and_tables.json'
});
//====
//! Menus
//====
var Menus = Backbone.Collection.extend({
url: 'api/menus_and_tables.json'
});
我的 json :
{
"id" : 1,
"tables" : [{
"name": "Table 1",
"id": 1
}],
"menus" : [{
"name": "Main Menu",
"id": 1
}]
}
我的第一个表视图:
var TablesView = Backbone.View.extend({
events: {
"click li.tableli" : "openMenuItem"
},
openMenuItem: function(e){
currentLink = $(e.currentTarget);
tableId = currentLink.data('table_id');
app.navigate("table/" + tableId + "/menus");
console.log("table/" + tableId + "/menus");
},
initialize:function () {
this.render();
},
render:function () {
var that = this;
var tables = new Tables();
tables.fetch({
success: function (tables) {
var template = _.template($('#table-template').html(), {tables: tables.models});
that.$el.html(template);
}
})
}
});
我的第二个菜单视图:
var MenusView = Backbone.View.extend({
events: {
"click li.menucls" : "openMenuItem"
},
openMenuItem: function(e){
currentLink = $(e.currentTarget);
menuId = currentLink.data('menu_id');
console.log("menuId: " + menuId );
},
initialize:function () {
this.render();
},
render:function () {
var that = this;
var menus = new Menus();
menus.fetch({
success: function (menus) {
var template = _.template($('#menu-template').html(), {menus: menus.models});
that.$el.html(template);
}
})
}
});
全部呈现到我的内容 div :
<header class="header"></header>
<div class="container">
<div class="row-fluid">
<div id="content" class="span12>">
<!- There is rendered content ->
</div>
</div>
</div>
<footer class="footer"></footer>