compound icon indicating copy to clipboard operation
compound copied to clipboard

How can use underscorejs in view ?

Open daveliu opened this issue 11 years ago • 7 comments

in express , it like this

app.locals._ = require("underscore");  

then can use _. in views , how can do it in compound?

daveliu avatar Oct 11 '13 07:10 daveliu

You still can use expressjs way, but i wouldn't recommend you to use underscore in views. Anyway, this is a code that will add _ to both controller and views:

    app.locals._ = compound.controllerExtensions._ =

require('underscore');

put this line to config/environment.js

On Fri, Oct 11, 2013 at 8:10 AM, liu wen ju [email protected]:

in express , it like this

app.locals._ = require("underscore");

then can use _. in views , how can do it in compound?

— Reply to this email directly or view it on GitHubhttps://github.com/1602/compound/issues/580 .

anatoliychakkaev avatar Oct 11 '13 08:10 anatoliychakkaev

thanks! Can you explain why don't recommend use underscore in views?

daveliu avatar Oct 12 '13 06:10 daveliu

I puts this in config/environment.js, but still not work , it show me "_ is not defined" , is something wrong?

Thanks.

daveliu avatar Oct 12 '13 06:10 daveliu

I use "express": "~3.x" , "compound": ">= 1.1.0"

daveliu avatar Oct 12 '13 06:10 daveliu

Working fine for me. Show me your code.

On Sat, Oct 12, 2013 at 7:10 AM, liu wen ju [email protected]:

I use "express": "~3.x" , "compound": ">= 1.1.0"

— Reply to this email directly or view it on GitHubhttps://github.com/1602/compound/issues/580#issuecomment-26191823 .

Thanks, Anatoliy Chakkaev

anatoliychakkaev avatar Oct 12 '13 11:10 anatoliychakkaev

Here is my config/environment.js


module.exports = function (compound) {

    var express = require('express');
    var app = compound.app;
    var _ = require('underscore');

    require('./mongoose').init(compound);    

    app.locals._ = compound.controllerExtensions._ = require('underscore');

    app.configure(function(){
        app.use(express.static(app.root + '/public', { maxAge: 86400000 }));
        app.set('jsDirectory', '/javascripts/');
        app.set('cssDirectory', '/stylesheets/');
        app.set('cssEngine', 'stylus');

        app.set('view engine', 'jade')

        compound.loadConfigs(__dirname);
        app.use(express.bodyParser());
        app.use(express.cookieParser('secret'));
        app.use(express.session({secret: 'secret'}));
        app.use(express.methodOverride());
        app.use(app.router);



    });

};


daveliu avatar Oct 14 '13 02:10 daveliu

And view code?

On Mon, Oct 14, 2013 at 3:02 AM, liu wen ju [email protected]:

Here is my config/environment.js

module.exports = function (compound) {

var express = require('express');
var app = compound.app;
var _ = require('underscore');

require('./mongoose').init(compound);

app.locals._ = compound.controllerExtensions._ = require('underscore');

app.configure(function(){
    app.use(express.static(app.root + '/public', { maxAge: 86400000 }));
    app.set('jsDirectory', '/javascripts/');
    app.set('cssDirectory', '/stylesheets/');
    app.set('cssEngine', 'stylus');

    app.set('view engine', 'jade')

    compound.loadConfigs(__dirname);
    app.use(express.bodyParser());
    app.use(express.cookieParser('secret'));
    app.use(express.session({secret: 'secret'}));
    app.use(express.methodOverride());
    app.use(app.router);



});

};

— Reply to this email directly or view it on GitHubhttps://github.com/1602/compound/issues/580#issuecomment-26232416 .

Thanks, Anatoliy Chakkaev

anatoliychakkaev avatar Oct 14 '13 09:10 anatoliychakkaev