Hopefully this isn't too stupid question as I attempt to get my head around node.
I've been looking at the express framework and am trying to get my head around the simple problem of a create/edit view.
If I have a jade view as below:
input(name='bar', id='bar', value=#{foo.bar})
The view expects the property foo.bar on the model I pass back, fine for an edit screen. However if this is a create screen I don't really want to have to pass back an empty model. How should this be dealt with? Is it just a case that I have to pass back an empty model? If so any pointers to a pattern for this would be great.
Thanks.
(I know I'm missing something fundamental)
I've done this myself by simply always making sure that there's a foo-object, and then referencing foo.bar anyway. This can be done in a lot of ways. You could hijack res.render or use your own custom render method, like so (somwhere when initalizing):
express.response.renderFooAdmin = function (view, data, callback) {
data.foo = data.foo || {};
this.render(view, data, callback);
};
And in your route:
res.renderFooAdmin('view', data);
And reference the object directly instead of via a #{} block to not print "undefined":
input(name='bar', id='bar', value=foo.bar)
You can use that. You still have to pass an empty foo here. You may want to check typeof foo != null if you want to avoid that
input(name='bar', id='bar', value=foo ? foo.bar : 'default')