从Backbone.Router模板访问res.locals [英] Accessing res.locals from Backbone.Router templates

查看:149
本文介绍了从Backbone.Router模板访问res.locals的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直对这样一个非常基本的问题的NodeJS太长,在这一点上,我很坚持。还有人问类似的问题,但由于某种原因,答案似乎并不为我工作。

I've been working on a very basic nodejs problem for way too long at this point and I'm stuck. Others have asked similar questions but for some reason the answers don't seem to work for me.

我只是在斜坡向上与防爆preSS,骨干,与放大器的NodeJS;护照身份验证。护照存储在请求的用户对象,所以我想将它传递到我的骨干看法。

I'm just ramping up with NodeJS with Express, Backbone, & Passport for auth. Passport stores the user object in the request so I'm trying to pass it up to my Backbone views.

我一直在使用节点酒窖项目为起点,并跟着他们在通过Backbone.Router.extend显示我的大多数看法领先。

I've been using the Node Cellar project as a starting point and have followed their lead in displaying most of my views via Backbone.Router.extend.

我只是想给用户对象传递到一个视图和显示器,例如在我的模板的名称。

I just want to pass the user object to a view and display, e.g. a name in my template.

我使用类似于下面的中间件来我的用户传递给模型

I'm using middleware similar to the below to pass my user to the model

app.use(function(req, res, next){
   res.locals.user = req.user;
   next();
});

当我使用app.get直接我能够证实这个作品:

When I use app.get directly I'm able to confirm that this works:

app.get('/test',function index(req, res) {
    res.send(res.locals.user);
});

不过,如果我包括我的HeaderView.html以下文件都坏了(即使我可以确认用户通过app.get定义):

However, if I include the following in my HeaderView.html file everything breaks down (even if I can confirm user is defined via app.get):

<%= user === null ? "No user" : user %> (or user.name.familyName, etc.)

我在控制台中出现以下错误:

I get the following error in the console:

Uncaught ReferenceError: user is not defined

下面就是我的main.js如下:

Here's what my main.js looks like:

var AppRouter = Backbone.Router.extend({
    routes: {
        "home"               : "home"  
    },

initialize: function () {
    this.headerView = new HeaderView();
    $('.header').html(this.headerView.el);

    var footerView = new FooterView();
    $('#footer').html(footerView.el);
},

home: function (id) {
    if (!this.homeView) {
        this.homeView = new HomeView();
    }
    $('#content').html(this.homeView.el);
},

});

utils.loadTemplate(['HomeView'], function() {
    app = new AppRouter();
    Backbone.history.start();
});

下面是如何我设立前preSS:

Here's how I'm setting up express:

app.set('port', process.env.PORT || 3000);
app.use(express.logger('default'));  /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser()),
app.use(express.static(path.join(config.root, 'public')));
app.use(express.cookieParser('optional secret string'));
app.use(express.session({ secret: 'my secret secret key' }));
app.use(passport.initialize());
app.use(passport.session());

有什么想法?

推荐答案

我收回我的preVIEW的答案,因为这是短视的:)

I withdraw my preview answer, as it was shortsighted :)

看起来你可以在这里找到答案:<一href=\"http://stackoverflow.com/questions/12550067/ex$p$pssjs-3-0-how-to-pass-res-locals-to-a-jade-view\">Ex$p$pssJS 3.0如何res.locals传递给玉看法?

It looks like you can find your answer here: ExpressJS 3.0 How to pass res.locals to a jade view?

心连心,结果
阿龙

hth,
Aaron

这篇关于从Backbone.Router模板访问res.locals的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆