我正在尝试重置一个集合,但是当我这样做时,我收到以下错误,
未捕获的TypeError:无法读取属性'原型'未定义的
触发此重置的代码如下,
// Main object for the entire app
window.APP = {
config: {
api: {
base:'http://app.local/api/v1/',
}
},
// Create this closure to contain the cached modules
module: function() {
// Internal module cache.
var modules = {};
// Create a new module reference scaffold or load an
// existing module.
return function(name) {
// If this module has already been created, return it.
if(modules[name]) {
return modules[name];
}
// Create a module and save it under this name
modules[name] = { Views: {} };
return modules[name];
};
}(),
init: function() {
// :: app start :: //
var app = POPS;
var Module = app.module( $( '#popsapp' ).data('route') );
// Creates a Master object in the global namespace so data can be passed in from the DOM.
// This would be replaced with a master Router if we weren't using actual pages
app.Initialiser = function( initialCollection ) {
this.start = function() {
//don't cache ajax calls
$.ajaxSetup({ cache: false });
console.log(Module);
if(Module.Collection !== undefined) {
this.collection = new Module.Collection();
this.view = new Module.Views.Master({ collection: this.collection });
} else {
this.view = new Module.Views.Master();
}
if(this.collection !== undefined) {
console.log(this.collection);
console.log(initialCollection);
console.log(this.collection.reset( initialCollection ));
this.collection.reset( initialCollection );
}
//moved this here so script runs after the DOM has loaded.
//but script.js still needs completely removing.
};
};
}
};
//进入应用程序的入口点 APP.init();
如果我在this.collection.reset( initialCollection)
之前登录,则会在this.collection
处触发错误。我可以看到一个空的集合。我试图创建我的集合的JSON如下,
[
{
"name": "Organisation 1",
"information": "This is some information about the organisation!",
"notifications": "0",
"add_all": "0",
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00",
"type": "organisation",
"clients": [
{
"id": "1",
"name": "Client 1",
"information": "A client called Client 1",
"add_all": "0",
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00",
"type": "client",
"pivot": {
"organisation_id": "1",
"client_id": "1"
}
},
{
"id": "2",
"name": "Client 2",
"information": "A client called Client 2",
"add_all": "1",
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00",
"type": "client",
"pivot": {
"organisation_id": "1",
"client_id": "2"
}
}
],
"projects": [
{
"id": "1",
"name": "Project #1",
"description": "Descriptive text.",
"total_cost": "0.00",
"start_date": "2014-01-01",
"finish_date": "2014-12-31",
"run_number_days": "365",
"num_days_from_year_start": "0",
"color": "#92e807",
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00",
"user_id": "1",
"pivot": {
"organisation_id": "1",
"project_id": "1"
}
}
],
"members": [],
"teams": []
},
{
"name": "Organisation 2",
"information": "This is some information about Organisation 2",
"notifications": "0",
"add_all": "0",
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00",
"type": "organisation",
"clients": [],
"projects": [],
"members": [],
"teams": []
},
{
"name": "Client 1",
"information": "A client called Client 1",
"add_all": "0",
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00",
"type": "client",
"members": [
{
"id": "1",
"email": "user@domain.com",
"first_name": "User",
"last_name": "Name",
"display_name": "",
"initials": null,
"remember_me": null,
"login_type": "normal",
"api_token": null,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00",
"deleted_at": null,
"pivot": {
"client_id": "1",
"user_id": "1"
}
}
]
},
{
"name": "Client 2",
"information": "A client called Client 2",
"add_all": "1",
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00",
"type": "client",
"members": []
}
json完全有效,我不确定问题是什么。有没有人有任何想法,它看起来像this.collection
没有定义,这就是为什么我不能在它上面运行reset(),但我可以在登录时看到收藏品。
答案 0 :(得分:0)
collection.reset()
方法将Backbone模型数组作为输入。您的数组(initialCollection
)只是一个通用的JavaScript数组。