我有这个视图(ordersView),它在初始化时获取订单并在完成后重新呈现视图。el 属性是在路由器中实例化所述视图时定义的。我在实例化后调用渲染并显示#order el,但在 fetch el 之后未定义,这是为什么呢?
我的ordersView.js视图:
// Menu View
// =============
// Includes file dependencies
define([ "jquery",
'underscore',
"backbone",
'handlebars',
"collections/menu",
"collections/orders",
"collections/stores",
'hbs!template/ordersTemplate',
], function( $, _, Backbone, Handlbars, MenuCollection, OrdersCollection, StoresCollection, OrdersTemplate ) {
// Extends Backbone.View
var OrdersView = Backbone.View.extend( {
// The View Constructor
initialize: function() {
OrdersCollection.fetch({'success': this.render})
},
events: {
},
showMoreInfo: function(e) {
$(e.target).closest('.store').toggleClass('hideinfo');
},
// Renders all of the Category models on the UI
render: function() {
console.log('rendered');
console.log( this.$el );
this.$el.html( OrdersCollection.toJSON() );
}
} );
// Returns the View class
return OrdersView;
} );
这是我的router.js文件:
// Filename: router.js
define([
'jquery',
'underscore',
'backbone',
'../views/ordersView',
], function($, _, Backbone, OrdersView) {
var AppRouter = Backbone.Router.extend({
routes: {
// Default
'*actions': 'defaultAction'
}
});
var initialize = function(){
var app_router = new AppRouter;
app_router.on('route:defaultAction', function (actions) {
var ordersView = new OrdersView({ el: $("#orders") });
ordersView.render();
});
Backbone.history.start();
};
return {
initialize: initialize
};
});
我正在使用 JQuery jquery-1.10.2、Backbone.js 主干 AMD 1.0.0 和 RequireJS AMD 加载程序