2014-08-29 138 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 
相關問題