Mongoose questions about routing and creating new objects - node.js

I am new to Node, and I'm using Mongoose as a driver for MongoDB. I'm using Jade for my views.
My "Edit" views and my "Show Object" views are looking great. However, I'm getting an "object is not defined" error when trying to create a new object.
I have this in my server.js:
require('./models/object');
app.resource('objects', require('./routes/objects'))
and in my routes folder I have:
exports.new = function(req, res) {
res.render('object/new')
}
finally my view looks like this:
h1 New Object
form(method='post', action='/objects')
.formRow
label Name:
input(type='text', name='object[name]', value=object.name)
.formRow
label Email:
textarea(name='object[email]')= object.email
.formRow
input(type='submit', value='Create')
Is there something I am leaving out? Thank you for your help.

It seems that you are using the express framework
you need to pass in the object while rendering the view.
res.render('object/new', { object: 'your object here'})
you may see http://expressjs.com/guide.html#view-rendering for example

Related

how to store meteor helpers in mongoDB collections, and how we can rendered it from mongoDB collection

How to store meteor helpers in mongoDB collections, and how we can rendered it from mongoDB collection?
For example i have following field in mongo collection.
"templateName":"hello {{userName}} <div> Welcome to Store</div>"
in My .js file i want to do
userName:function(){
return "Foo";
}
Create a new collection to keep your templates:
MyTemplates = new Mongo.Collection('MyTemplates');
Insert your templates inside this collection.
In the template file which you want to load your templates stored in mongodb, write a helper as follows:
Template.YOURTEMPLATENAME.helpers({
'getMyDynamicTemplate': function () {
Template.hello = Template.fromString(MyTemplates.findOne({"templateName":"blablabla"}).templateString);
return 'hello';
}
});
Put below code in your template's html:
{{> Template.dynamic template=getMyDynamicTemplate}}
For Template.dynamic please refer here. And lastly refer here for populating your template from string
I had a similar issue and worked it out using Servan's answer as the base. THANK YOU!!!
It didn't work form me with findOne, so I changed it to find(...).fetch().
First I added the package:
meteor add numtel:template-from-string
My Helper was:
Template.YOURTEMPLATENAME.helpers({
'getMyDynamicTemplate': function () {
var MyHTML = MyTemplates.find({},{HTML:1}).fetch();
Template.hello = Template.fromString(MyHTML[0].HTML);
return 'hello';
}
});
And then put this code in my template's html:
{{> Template.dynamic template=getMyDynamicTemplate}}
It worked out beautifully!

Express middleware separate instances

I am working on a personal project for a photography web application.
Still very new to node.js and express.
The previous ASP.Net MVC app I built for this application used a SQL database to index photos and auto-generate the neccesary photo gallery markup.
node-gallery
seemed like a good candidate to remove the db dependency.
However I seem to be confused as to how Express middleware works.
var nodeGallery = require('node-gallery');
//
//
app.use('/', new nodeGallery({
staticFiles : 'public/photos/slideshow',
urlRoot : 'photos/slideshow',
render : false
}), function(req, res, next){
rerurn res.render('index',{slideshow:req.data});
});
app.use('/portfolio', new nodeGallery({
staticFiles : 'public/photos/portfolio',
urlRoot : 'portfolio',
render : false
}), function(req, res, next){
console.log(req.data);
return res.render('portfolio',{portfolio:req.data});
});
I am wanting to use the node-gallery middleware with different properties for two pages (the front with a slideshow and the main gallery). However the last properties set are always used regardless of route.
Using express.Router and specifying a route which uses the middleware also appears to not work.
It seems like I am missing something basic here. Any suggestions would be appreciated.
var nodeGallery = require('node-gallery');
Think of this as creating a class/obj of node gallery.
Then in your first route you set its properties as:
new nodeGallery({
staticFiles : 'public/photos/slideshow',
urlRoot : 'photos/slideshow',
render : false
})
but in the second route you overwrite nodeGallery with a second set of properties:
new nodeGallery({
staticFiles : 'public/photos/portfolio',
urlRoot : 'portfolio',
render : false
})
The problem is you are using the same instance of nodeGallery, to define to sets of different properties.
Solution:
1. Create an object of multiple node-galleries i.e:
var nodeGallery = {
slideShow: require('node-gallery'),
mainGallery: require('node-gallery'),
}
and then access them as nodeGaller.slideShow({staticFiles....})
2. the better solution in my opinion would be to use the recommended way on the github page:
app.use('/gallery', require('node-gallery')({
staticFiles : 'resources/photos',
urlRoot : 'gallery',
title : 'Example Gallery'
}));
you're not setting node-gallery to any one variable, just initializing for that route.

call multi layout for controller in sails.js

SAILS.JS
I have two forder in controller: admin and public.
I want to edit view.js file in config forder.
if controllers file in admin forder, it call a layout: layout-admin
if controllers file in public forder, it call a layout: layout-public
but i don't know do it.
please support for me with this. thank a lot!
You can do what you want, look the doc here : http://sailsjs.org/documentation/reference/configuration/sails-config-views
The layout attributs can only be a string or a boolean, there no way actually to define a layout with a function or for an entire controller.
You can make a feature request to sails to see this feature in a next version.
You can specify layout file in your controller like this :
myAction : function (req, res)
{
var layout = "layout-public";
if(req.session.authenticated)
{
layout = "layout-admin";
}
res.view("myview", {
layout : layout
});
}

How to return invalid form fields back to the page?

It's a classic case of create and edit a form data. I got a complex form that suppose to render back all fields to validate it, but doesn't works. For this case, I've created a simple example to illustrate my concern and try to understand what's happing.
In this sample app body parser and method override are up:
app.use(express.bodyParser());
app.use(express.methodOverride());
Now I have a simple form in user.jade:
doctype 5
html
header
title Add new user
body
h4 User - Add
form(id='userForm', method='post', action='/user/save')
label(for='name') Name
input(type='text', id='name', name='name')
label(for='address') Address
input(type='text', id='address', name='address')
input(type='submit', value='Save')
The GET and POST HTTP Methods are configured:
app.get('/user/new', user.create); //create new user
app.post('/user/save', user.save); //save or edit user in validation process
Finally the "create" and "edit/save" functions:
exports.create = function(req, res) {
res.render('user');
};
exports.save = function(req, res) {
var name = req.body.name;
var address = req.body.address;
//do some validation
//...
//return it back to page to fix wrong fields
res.render('user', { name: name, address: address });
};
Why I render user.jade back, so the fields are not filled? Can't use res.render to fill page form?
Thanks in advance!
In user.jade you aren't using #{name} or #{address}
try something like this to access the local variable
input(type='text', id='name', name='name', value=#{name})

how to i mplement autocomplete using yui

As am totally new to YUI i dont have any clue about.I have just gone through this link to implement autocomplete using YUI http://developer.yahoo.com/yui/autocomplete/.
According to my requirement i need to assign a string array dynamically to datasource object instead of
var dsLocalArray = new YAHOO.util.LocalDataSource(["apples", "broccoli", "cherries"]);
something like
var dsLocalArray=new YAHOO.util.LocalDataSource(documentList[]);
where my documentList is String Array.How do i that?Thanks in advance for the help.
I would suggest you to use YUI3 than YUI2, the example you are showing which uses the YAHOO namespace which is YUI2.
YUI3 is simpler and better, you can get the docs here:
http://yuilibrary.com/yui/docs/autocomplete/
Example of implementing with YUI3 including highlighting feature:
YUI().use('autocomplete', 'autocomplete-filters', 'autocomplete-highlighters', function (Y) {
Y.one('#ac-input').plug(Y.Plugin.AutoComplete, {
resultFilters : 'phraseMatch',
resultHighlighter: 'phraseMatch',
source : ['Alabama','Alaska','Arizona','Arkansas','California']
});
});
Try to lok into the examples at the right bottom side panel in the above docs link.

Resources