ericf / express-handlebars

A Handlebars view engine for Express which doesn't suck.
BSD 3-Clause "New" or "Revised" License
2.31k stars 383 forks source link

Uncaught Error: You must pass a string or Handlebars AST to Handlebars.compile. You passed undefined #67

Closed cherrypophead closed 10 years ago

cherrypophead commented 10 years ago

My apologies if this has been resolved. I'm a noobie to handlebars and can't figure out from the forums how to resolve my issue. I'm trying to add an edit feature to a list for an html5 mobile app taken from a video tutorial. Handlebars seems to want a string, but I'm not sure of where Im supposed to pass this string. What in sam's name am I doing wrong? If someone can provide some insight - i'd be really grateful. Thanks, Renae. Here's a part of the script:

the MAIN.JS file is:

'use strict'; var beers;

//functions var hideShowFunction = function(evt){ var eventTarget = $(evt.target); var data = eventTarget.data(); $('.view').removeClass('active'); $(data.target).addClass('active');

renderTemplate(data.target, data.item);

};

var renderTemplate = function(target, itemId) { var targetTemplate = target + '-template'; var source = $(targetTemplate).html(); var template = Handlebars.compile(source); // var data = {beers: beers}; // var data = {title: 'This is the title', body: 'This is the body!'}; // actual data! var data; if (itemId){ var arr = $.grep(beers, function(beer, i){ return beer.id == itemId; }); data = arr[0]; } else { data = {beers: beers}; } var html = template(data); $(target).html(html);

};

$.fn.serializeObject = function(){ var o = {}; var a = this.serializeArray(); $.each(a, function() { if (o[this.name] !== undefined) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; };

var saveEntry = function(result) { var highestId = Math.max.apply(Math, beers.map(function(o){return o.id;})); result.id = highestId > 0 ? highestId + 1 : 1; beers.push(result); localStorage.beers = JSON.stringify(beers); };

var editEntry = function(result) { var resultInt = parseInt(result.id); result.id = resultInt; for(i in beers) { if (beers[i].id === resultInt ) { beers[i] = result; localStorage.beers = JSON.stringify(beers); break; } } };

// event listeners $('body').on('click', '.view-switcher', function(evt){ hideShowFunction(evt); });

$('body').on('submit','#add-form', function(evt){ var result = $(this).serializeObject(); saveEntry(result); renderTemplate('#full-list'); return false; });

$('body').on('submit','#edit-form', function(evt){ var result = $(this).serializeObject(); editEntry(result); renderTemplate('#full-list'); return false; });

//storage if (Modernizr.localstorage) { if (localStorage.beers){ beers = JSON.parse(localStorage.beers); } else { beers = []; } } else { console.log('does not work'); }

// call render template (init) renderTemplate('#full-list');

ericf commented 10 years ago

This package only works server-side in Node.js. From the code you posted, it looks like you're asking about using Handlebars client-side. Not sure I can help with this, you should ask on Stack Overflow since this is a generic Handlebars question.

cherrypophead commented 10 years ago

We'll do, thanks, Eric.

On Tue, Jul 8, 2014 at 9:54 PM, Eric Ferraiuolo notifications@github.com wrote:

This package only works server-side in Node.js. From the code you posted, it looks like you're asking about using Handlebars client-side. Not sure I can help with this, you should ask on Stack Overflow http://stackoverflow.com since this is a generic Handlebars question.

— Reply to this email directly or view it on GitHub https://github.com/ericf/express3-handlebars/issues/67#issuecomment-48429968 .