質問 21560374に似ています。
Backbone.Paginatorバージョン 0.8を実装しようとしていますが、ページの読み込み時にエラーが発生します: Uncaught TypeError: Cannot read property 'requestPager' of undefined.
最初に CDNJS ファイルを参照し、生のコードをダウンロードして app/assets/javascripts に配置しました。また、application.js '//= require backbone.paginator.min.js' でそれを要求しました。
application.html.erb にある my セクション コードは次のとおりです。
<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;
}
どんな助けでも大歓迎です!