过去,我一直在做的是,当我想用模板渲染和显示集合时,只需调用集合上的“每个”方法。对于每个模型,我为每个模型创建一个新的单一视图并将其附加到 $el. 但在这种情况下,这是行不通的。我的模板中有一个输入类型文本,我不想为每个模型显示一个新输入。我宁愿以某种方式将集合传递给模板并迭代模板中的集合。另外值得注意的是,在创建此视图的新实例时,我的集合是通过我的主应用程序视图传入的:
// clientsview.js
define(
[
'backbone',
'ClientView',
'Clients'
], function(Backbone, ClientView, Clients) {
ClientsView = Backbone.View.extend({
tagName: '#page',
template: _.template( $('#allClientsTemplate').html() ),
initialize: function() {
this.render();
},
render: function() {
// var that = this;
console.log('collection');
console.log(this.collection);// successfully returns collection
console.log('template');
console.log(this.template);// successfully returns template function
// This causes an error:
//Uncaught TypeError: Cannot call method 'toJSON' of undefined
var tmpl = this.template({ clients: this.collection.toJSON() });
return this;
}
});
return ClientsView;
});
我在 index.html 中的模板
// index.html
<script id="allClientsTemplate" type="text/template">
// I do not want create an input for every single model
// but I need the input to be part of the view.
<input type="text" class="table-search" id="search" autocomplete="off" placeholder="Search Clients…">
<table class="table" id="tblData">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Status</th>
</tr>
</thead>
<tbody id="tblDataBody">
<% _.each(clients, function(client) { %>
<tr>
<td><a href=""><%- client.first_name %></a></td>
<td><a href=""><%- client.last_name %></a></td>
<td><a href=""><%- client.status %></a></td>
</tr>
<% }); %>
</tbody>
</script>
我正在从我的主应用程序视图中实例化我的客户视图:
define(
[
'jquery',
'underscore',
'backbone',
'AddClientView',
'ClientsView',
], function( $, _, Backbone, AddClientView, ClientsView ) {
AppView = Backbone.View.extend({
initialize: function() {
// var addClientView = new AddClientView({ collection: this.collection });
var clientsView = new ClientsView({ collection:this.collection });
}
});
return AppView;
});
我正在从我的初始化方法实例化我的主应用程序视图:
define('App', [
'jquery',
'underscore',
'backbone',
'Router',
'bootstrap',
'Clients',
'AppView'
], function($, _, Backbone, Router, bootstrap, Clients, AppView) {
function initialize() {
// for custom global events
window.vent = _.extend( {}, Backbone.Events )
var app = new Router();
Backbone.history.start();
// probably more appropriate to do this in app view
var clients = new Clients;// Get collection of clients
clients.fetch().then( function() {// fetch client collection from db
new AppView({ collection: clients });
});
}
return {
initialize: initialize
};
});
我的初始化方法是从 main.js 调用的:
// main.js
require.config({
shim: {
'underscore': {
exports: '_'
},
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
'bootstrap': {
deps: ['jquery'],
exports: 'bootstrap'
}
},
paths: {
'text' : 'lib/require/text',
'jquery' : 'lib/jquery/jquery-1.9.1',
'underscore' : 'lib/underscore/underscore',
'backbone' : 'lib/backbone/backbone',
'bootstrap' : 'lib/bootstrap/bootstrap',
'App' : 'app',
'Router' : 'router',
'AppView' : 'views/app/appview',
'Client' : 'models/clients/client',
'Clients' : 'collections/clients/clients',
'ClientView' : 'views/clients/clientview',
'ClientsView' : 'views/clients/clientsview',
'AddClientView' : 'views/clients/addclientview'
}
});
require(['App'], function(App) {
App.initialize();
});
当我尝试这种方法时出现错误(未捕获的类型错误:无法调用未定义的方法 'toJSON')。我怎样才能使这项工作?