rendrjs / rendr-app-template

Basic skeleton for a Rendr app. Deprecated in favor of ./examples dir in rendr repo.
MIT License
285 stars 83 forks source link

initialize model returns error #25

Closed grudelsud closed 11 years ago

grudelsud commented 11 years ago

hey guys,

first of all: great stuff, thank you very much for opensourcing this. hope this is the right place to post this issue.

I've updated to repo's head and my initialize function in a model stopped working giving 500 TypeError: Cannot read property 'req' of undefined

the model works correctly setting default values without defining the initialize function, express starts giving errors as soon as iniitialize is defined, even if empty. any suggestion appreciated

spikebrehm commented 11 years ago

Hi, thanks for the issue report! Probably the issue is that you need to call the model super's initialize method, because Rendr utilizes initialize to add some important functionality to BaseModel: https://github.com/airbnb/rendr/blob/master/shared/base/model.js#L11

In your model, you could do something like this:

var BaseModel = require('rendr/shared/base/model');
module.exports = BaseModel.extend({
  initialize: function() {
    BaseModel.prototype.initialize.apply(this, arguments);

    // your code here
  }
});
module.exports.id = 'MyModelName';

Give that a try. But this brings up a good point that Rendr should endeavor to not hijack the model's initialize method; instead we should just hijack the constructor. Will leave this open as a reminder to address that.

grudelsud commented 11 years ago

it works!

thank you very much man, really helpful. just so you know, I forked the rendr-app-template about a month ago and without any "super" initialization it worked just fine, the problem started after I synced with the latest updates yesterday, and now fixed calling the initialize function of the base model.

thank you again, closing this issue!

EvanCarroll commented 9 years ago

Where is this idiom of calling the BaseModel.prototype.initialize documented? I've seen lots of examples where this is never done...

# app/models/user

BaseModel = require('rendr/base/model')

module.exports = class User extends BaseModel
  url: '/users/:id'