I'm trying to find my way through node.js and backbone.js. My intention is to share at least all model-states with the server and browser. Right now I'm not quite sure if I really need to share views as well. I also want to handle all routes by express and nodejs and not backbone. To keep my code a wee bit more structured I was looking forward to keep each model in a separate *.js file. By doing so I'm running into following error message:
TypeError: Object #<Object> has no method 'extend'
I thought it might be a problem with underscore missing in the separate file, so here is my basemodel:
models/BaseModel.js
var Backbone = require('../node_modules/backbone'),
_ = require('../node_modules/underscore'),
var BaseModel = Backbone.Model.extend({
modelName: 'basemodel'
});
exports.BaseModel = BaseModel;
app.js var BaseModel = require('./models/BaseModel');
var MyModel = BaseModel.extend({
// ... attributes, functions etc.
});
Does anyone have a hint what I'm doing wrong there?
Then try this code in app.js:
BaseModel = require('./models/BaseModel').BaseModel;
Or in models/BaseModel.js not of exports.BaseModel = BaseModel; use it - module.exports = BaseModel
I actually figured it out. The flaw was in referencing to the BaseModel, so
this
var MyModel = BaseModel.extend({
//
});
had to turn into:
var MyModel = BaseModel.BaseModel.extend({
//
};
because of exports.BaseModel = BaseModel; in BaseModel.js