2014-08-29 135 views
0

我一直致力于将基本的基于Express的应用程序移植到Meteor。优秀的帖子Is there an easy way to convert an express app to meteor?是一个很好的开始,使用一个服务器功能来包装Meteor预期的铁路由器路由到Express喜欢的req/res。将移动应用程序移植到流星

但是,我遇到了一个我坚持的错误。我无法让Meteor将res.render对象传递给我的把手模板引擎。

例如:

main.js

app.get('/complex', function(req, res){ 
    var data = { 
    name: 'Gorilla', 
    address: { 
     streetName: 'Broadway', 
     streetNumber: '721', 
     floor: 4, 
     addressType: { 
     typeName: 'residential' 
     } 
    } 
    }; 
res.render('complex', data); 
}); 

当/复杂路径经由铁路由器调用时,它被路由到功能res.render下面

/** create an sync version for meteor */ 
waiter = function(foo, req, res) { 
    var waiter_aux = Meteor._wrapAsync(function(foo, req, res, callback) { 

    res.set = function(header, value) { 
     res.setHeader(header, value); 
    }; 

    res.send = function(codeorhtml, html) { 
     if (html) { 
      // two arguments provided, treat as described 
      res.statusCode = codeorhtml; 
     } else { 
      // no code, just html 
      html = codeorhtml; 
     } 
     callback(null, html); 
    }; 

    res.render = function(name, data, callback) { 
     callback = callback || function(err, html) { 
      res.send(html); 
     }; 

     console.log(name); // complex 
     console.log(data); // Gorilla... 
     var html = Handlebars.templates[name](data); // THIS ERRORS OUT 
     html = JSON.stringify(name) + " " + JSON.stringify(data) // WORKS 
     callback(null, html); 
    }; 
    ... 

在上面的消息中,编译器错误地表示Handlebars未定义。

W20140828-22:47:49.439(-7)? (STDERR) TypeError: Cannot call method 'complex' of undefined 
W20140828-22:47:49.439(-7)? (STDERR)  at ServerResponse.res.render (app/server/myapp.js:57:50) 
W20140828-22:47:49.440(-7)? (STDERR)  at app/server/myapp.js:298:25 

我用NPM的车把包建预编译模板(下面的例子),但我没有任何运气得到它才能正常工作

(function() { 
    var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {}; 
    templates['complex'] = template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) { 
    var helper, functionType="function", helperMissing=helpers.helperMissing, escapeExpression=this.escapeExpression; 
    return "\n<p>\nThe data that was passed to `res.render` is:\n<code>var data = {name: 'Gorilla'};</code>\n</p>\n\n<p>\nWe can display the value of <em>name</em> using <code>&#123;&#123;name&#125;&#125;</code>, which results in: <b>" 
    + escapeExpression(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : helperMissing),(typeof helper === functionType ? helper.call(depth0, {"name":"name","hash":{},"data":data}) : helper))) 
    + "</b>.\n</p>\n"; 
},"useData":true}); 
})(); 

即使去定义的最简单途径本地模板

query_string = "<code>var data = {name: 'Gorilla'};</code><p>{{data}}</p>" 
template = Handlebars.compile(query_string) 

导致错误:

W20140828-21:51:47.126(-7)? (STDERR) TypeError: Object function exphbs(config) { 
W20140828-21:51:47.128(-7)? (STDERR)  return exphbs.create(config).engine; 
W20140828-21:51:47.129(-7)? (STDERR) } has no method 'compile' 

有关我如何成功地将JSON文档对象传递给Handlebars以在Meteor/Express内渲染的任何建议或示例,将不胜感激。理想情况下,我想使用实时部分,而不是简单的预编译代码。谢谢!!!

回答

1

如果这是一个基本的应用程序,我建议刚刚开始,重复使用的东西,你可以。通过为您的项目创建异步包装,您将增加很多复杂性。

0

为了让我以前的答复工作,你现在需要添加handlebars-server

meteor add cmather:handlebars-server 
相关问题