I am trying to reset a collection but when I do I get the following error,
Uncaught TypeError: Cannot read property 'prototype' of undefined
The code the fires this reset, is as follows,
// 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.
};
};
}
};
// Entry point into the application APP.init();
The error fires at this.collection.reset( initialCollection)
if I log this.collection
before that I can see a collection that is empty. The JSON I am trying to create my collection from is as follows,
[
{
"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": "[email protected]",
"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": []
}
]
The json is perfectly valid, I am not sure what the problem is. Does anyone have any ideas, it reads like this.collection
is not defined and that is why I can't run reset() on it, but I can see the colleciton when I log it.
The collection.reset()
method takes as input an array of Backbone models. Your array (initialCollection
) is just a generic JavaScript array.