我正在尝试实现Backbone.Paginator版本0.8并且在页面加载时我遇到了错误:Uncaught TypeError:无法读取未定义的属性'requestPager'。
首先我提到了CDNJS文件,然后我下载了原始代码并将其放在app / assets / javascripts中。我还在application.js'// = require backbone.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,但似乎在window initialize函数创建新路由器并检查我创建的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;
}
任何帮助将不胜感激!
答案 0 :(得分:0)
确保在启动应用之前包含骨干分页器库。弹出错误后可以访问requestPager
的原因可能是因为在该阶段已经加载了lib。
要确保这不是您的代码的问题,请尝试在主页<head>
部分中包含主干分页器代码。
根据评论进行编辑:
您需要更改“app / assets / javascripts / application.js”,以便按正确顺序包含文件。类似的东西:
//= require jquery
//= require jquery_ujs
//= require underscore
//= require backbone
//= require bacbkone.paginator.min
//= require_tree .
如果您只是依靠require_tree
来包含这些文件,那么它们不一定会包含在正确的顺序中,这正是您所遇到的。
此外,由于Backbone paginator包含在应用程序文件中,因此它应该不再存在。