类似于问题 21560374。
我正在尝试实现Backbone.Paginator版本 0.8,并且在页面加载时遇到错误:未捕获的类型错误:无法读取未定义的属性“requestPager”。
首先我参考了 CDNJS 文件,然后我下载了原始代码并将其放在 app/assets/javascripts 中。我还在 application.js '//= 需要主干.paginator.min.js' 中要求它。
这是位于 application.html.erb 中的我的部分代码:
<head>
<title>D2jive</title>
<link href='http://fonts.googleapis.com/css?family=Audiowide' rel='stylesheet' type='text/css'>
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
<script type="text/javascript" src="assets/backbone.paginator.min.js"></script>
<%= csrf_meta_tags %>
</head>
在窗口我实例化我的路由器:
window.D2Jive = {
Views: {},
Routers: {},
Events: {},
Models: {},
Collections: {},
initialize: function() {
D2Jive.router = new this.Router();
Backbone.history.start({pushState: true});
},
};
位于我的路由器中,我指的是我的 PaginatedCollection:
this.collection = new D2Jive.Collections.PaginatedCollections( [], { location: location });
然后在 Backbone.Paginator.requestPager 的实际代码出现的地方弹出错误。
D2Jive.Collections.PaginatedCollection = Backbone.Paginator.requestPager.extend({
initialize: function(attributes, options){
this.city = options.location;
},
我仍然习惯于 JavaScript,但似乎 Backbone.Paginator 代码在窗口初始化函数创建一个新路由器并检查我创建的 Backbone.Paginator 集合之后加载。我只是不确定如何避免这种情况。
错误弹出后,我可以在控制台中加载 Backbone.Paginator.requestPager。
Backbone.Paginator.requestPager.extend
function (protoProps, staticProps) {
var parent = this;
var child;
// The constructor function for the new subclass is either defined by you
// (the "constructor" property in your `extend` definition), or defaulted
// by us to simply call the parent's constructor.
if (protoProps && _.has(protoProps, 'constructor')) {
child = protoProps.constructor;
} else {
child = function(){ return parent.apply(this, arguments); };
}
// Add static properties to the constructor function, if supplied.
_.extend(child, parent, staticProps);
// Set the prototype chain to inherit from `parent`, without calling
// `parent`'s constructor function.
var Surrogate = function(){ this.constructor = child; };
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate;
// Add prototype properties (instance properties) to the subclass,
// if supplied.
if (protoProps) _.extend(child.prototype, protoProps);
// Set a convenience property in case the parent's prototype is needed
// later.
child.__super__ = parent.prototype;
return child;
}
任何帮助都感激不尽!