Uncaught TypeError: Cannot read property 'idAttribute' of undefined - backbone.js

I am using Backbone-Relational, and I recieve the following error:
Uncaught TypeError: Cannot read property 'idAttribute' of undefined
When I access the show_note route.
App.Router = Backbone.Router.extend({
routes: {
'note/:_id': 'show_note'
},
show_note: function(_id) {
console.log('this is the show_note route for _id: ' + _id);
var note = new App.Models.Note.findOrCreate({ _id: _id });
var note_view = new App.Views.main_note({ model: note });
note.fetch();
}
});
The console.log recieves the '_id'. But when I attempt to instantiate var note, I recieve the error. How do I fix this?
EDIT 1
Adding the Note model:
App.Models.Note = Backbone.RelationalModel.extend({
urlRoot: '/notes',
idAttribute: '_id',
relations: [{
type: Backbone.HasMany,
key: 'tags',
relatedModel: 'App.Models.Tag',
reverseRelation: {
key: 'note',
includeInJSON: '_id'
}
}]
});
EDIT 2
Stacktrace added

I solved the issue by removing findOrCreate(), and ended up retrieving the model data like so:
App.Router = Backbone.Router.extend({
routes: {
'note/:_id': 'show_note'
},
show_note: function(_id) {
var note = new App.Models.Note({ _id: _id });
note.fetch({
success: function(data) {
var mainNoteView = new App.Views.main_note({ model: note });
$('#mainNote').append( mainNoteView.render().el );
},
error: function(err) {
console.log(err);
}
});
}
});

Related

Append within Multiple Arrays using Mongoose

i am trying to handle list of friends or users which consists of comma separated ObjectID's of different users, now i have NetworkList array which stores, list of users, so far i used $push to append list of ObjectID's in NetworkList array here:
Model:
var NetworkSchema = new Schema({
UserID: {
type: String,
default: '',
trim: true
},
NetworkList: [{
type: Schema.ObjectId,
ref: 'User'
}]
});
Server Controller:
exports.update = function(req, res) {
var query={'UserID': req.body.UserID};
var update = {$push: {'NetworkList': req.body.FriendID}};
};
Now my model is :
var NetworkSchema = new Schema({
UserID: {
type: Schema.Types.ObjectId,
ref: 'User'
},
NetworkList: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
NetworkRequest: [{
to:[{
type: Schema.Types.ObjectId,
ref: 'User'}],
from:[{
type: Schema.Types.ObjectId,
ref: 'User'
}]
}]
});
and i want to append inside both array's NetworkRequest and NetworkList, so i tried to use $push with both like
Server Controller:
exports.update = function(req, res) {
var query={'UserID':req.body.UserID};
var update = {$push: {'NetworkRequest':{to: req.body.FriendID, from: req.body.McReg}, 'NetworkList': req.body.FriendID}};
Network.find(query,function(err,user){
console.log(user);
if (err) {
return err;
} else {
console.log('no error');
}
});
Network.update(update,function(err){
if (err) {
return err;
} else {
console.log('Updated');
}
});
Document:
{
NetworkList: [
ObjectId("5490098c9f2652f01b3f68df"),
ObjectId("5490195f5afa90e01b500c37"),
],
NetworkRequest: {
_id: ObjectId("54902e9375fff9b01a007516"),
from: [ObjectId("5490195f5afa90e01b500c37")],
to: [
ObjectId("54883e606d574c000feccb08"),(//not able to append here)
]
},
UserID: ObjectId("5490195f5afa90e01b500c37"),
__v: 0,
_id: ObjectId("5490195f5afa90e01b500c38")
}
i used console.log(update); where i am able to get objectID's inside both array's, but finally values are not getting up to the database, where i am going wrong with $push?
To push inside sub Array, you should use dot operator for your NetworkRequest Array.
exports.update = function(req, res) {
var query={'UserID':req.body.UserID};
var update = {$push: {'NetworkRequest.to': req.body.FriendID, 'NetworkRequest.from': req.body.McReg}, 'NetworkList': req.body.FriendID}};
Network.update(query,update,{upsert:true}function(err){
if (err) {
return err;
} else {
console.log('Updated');
}
});
}

Backbone.js Models Uncaught TypeError: undefined is not a function

I am trying to create a Backbone Model.
However the as soon i create a object of Model, i get following error
Uncaught TypeError: undefined is not a function
on backbone.js files extend function.
Here is jsFiddle for it.
var ProjectModel = new Backbone.Model.extend({
idAttribute: '_id',
urlRoot: '/project',
defaults: function () {
return {
name: 'New Project',
description: 'Add Description here'
};
}
});
// var Projects = Backbone.Collection.extend({
// model: ProjectModel,
// url: '/project'
// });
// var projects = new Projects();
var obj = new ProjectModel({
name: 'test'
});
console.log(obj);
Remove the "new" that appears before your Backbone.Model.extend call - should look like this:
var ProjectModel = Backbone.Model.extend({
idAttribute: '_id',
urlRoot: '/project',
defaults: function () {
return {
name: 'New Project',
description: 'Add Description here'
};
}
});
By putting "new" there, you were returning an instance of a Backbone model, not a new constructor with which to make new model instances.

How can I set a dynamic className on a model(parse object) using parse.com with backbonejs

Basically I have a model/collection that I want to reuse. I have a couple different collections on parse.com 'https://api.parse.com/1/classes/foo' and 'https://api.parse.com/1/classes/bar'.
My Collection:
define([
"app",
"models/listModel",
], function (app, ListModel) {
var ListCollection = Parse.Collection.extend({
model: ListModel,
});
return ListCollection;
});
My Model:
define([
"app"
],
function(app) {
var ListModel = Parse.Object.extend({
className: null,
initialize: function(attrs, options) {
this.className = app.foobar ? 'foo' : 'bar';
}
});
return ListModel;
});
And then I create a new collection:
app.foobar = 'bar';
var collection = new ListCollection();
collection.fetch({
wait: true,
success: function(collection, response, error) {
console.log(collection);
}
});
And this is the error I get:
Uncaught Error: Parse.Object.extend's first argument should be the className.
That gets thrown before initialize is ever fired in the model.

With Backbone.Forms is there a way to set the select editor option after loading a collection

I have a form with a select editor type which uses a collection, but I have not found any way of setting the selected option after the data is loaded. I have not found anything in the documentation or any examples online.
...
country: {
title: 'Country',
type: 'Select',
options: function (callback) {
var result = [];
var states = new Countries();
states.fetch({
success: function (collection) {
_.each(collection.models, function (model) {
result.push({ 'val': model.get('id'), 'label': model.get('name') });
});
callback(result);
}
});
},
validators: ['required']
},
...
you can use
form.getEditor('country').setValue('YourVal')

Fetching data from model with backbone posts wierd parameters

I have created a model and collection like this:
var TestModel = Backbone.Model.extend({
initialize: function () {
},
defaults: {
'id': null,
'title': '',
'description': ''
}
});
TestCollection = Backbone.Collection.extend({
model: TestModel,
url: function () {
return 'Test/GetAll';
},
});
I then have a view I try to load like this:
var testCollection = new TestCollection();
var testListView = new TestListView({ collection: testCollection });
testCollection.fetch();
But it creates a request like:
Test/GetAll?_=1349272902901 which fails. Do any of you know why it appends this id-like parameter to the request?
Try:
testCollection.fetch({ cache: false });

Resources