lundi 25 mai 2015

Proper display of user editing form (CRUD model) with checkbox

I'm making a simple CRUD model with input checkbox. I have no problems on server side, everything is fine. I use NodeJS +MongoDB. But I have problem in editing existing user. When I edit an existing user with a checked checkbox( I get JSON object with parameter checked=true ) how should I display it using JS?
This is part of my users.js file in /routes/ folder

var express = require('express');
var router = express.Router();
var User = require('../../models/User');
var rest = require('restler');
router.get('/adduser', function(req, res){
  var user = new User();
  user.contacts.push({phone: '', email: ''});
  rest.get('http://localhost:3000/api/adduser').on('complete', function(data) {
    res.render('users/add', { title: 'Add New Users' , n: user});
  });
});

And this is views/users/fields.jade part of file for better understanding:

.form-group
label.col-sm-2.control-label(for='email') E-mail:
.col-sm-10
input(type="email", placeholder="email", name="email", value = n.contacts[0].email,required)
.form-group
.col-sm-offset-2.col-sm-10
input#enabled(type="checkbox",style='text-align: center; vertical-align: middle;',placeholder="", name="enabled", value = n.enabled)
| Enable user

So my problem is that I don't understand how I should display that checkbox is really checked when loading existing user.
If user is checked attribute n.enabled=true and if not n.enabled=false. So if user is checked on load of that user I need the input filed to be checked.
I've tried it to do the following way, but it wrote me that n wasn't defined...and I don't know how to pass n as the parameter for that function:

$(document).ready(function(){
  if(n.enabled=="true"){$("enabled").toggle(this.checked);}
});




Aucun commentaire:

Enregistrer un commentaire