mercredi 29 avril 2015

How do I get the current folder in NodeJS for a Browserify'd file that relies on a base file in another folder?

I'm using AmpersandJS and I'm trying to emulate the pod structure of Ember so that, instead of the typical folders-per-type structure:

app
|-- views
    |-- index.js
    |-- login.js
    |-- people.js
|-- templates
    |-- index.jade
    |-- login.jade
    |-- people.jade

you have folders-per-component instead:

app
|-- index
    |-- view.js
    |-- template.jade
|-- login
    |-- view.js
    |-- template.jade
|-- people
    |-- view.js
    |-- template.jade
|-- base-view.js

I'm using templatizer to compile my templates:

templatizer(__dirname + '/app', __dirname + '/app/templates.js');

Which will make the template object look something like this (simplified for sake of explanation):

{
    "index.template": "<div>index</div>",
    "login.template": "<div>login</div>",
    "people.template": "<div>people</div>"
}

In the base view, I have the following, which will return the template by convention:

var BaseView = AmpersandView.extend({
  template: function() {
    // pretend that I'm cleaning up __dirname here
    return templates[__dirname].template;
  }
});

and in my other views, I'm extending the base view:

// in app/index/view.js:
var IndexView = BaseView.extend({ ... });

The problem I'm running into is that, when the base view goes through Browserify, __dirname is set to the folder of the base view template, and not the view that's extending it. As a result, __dirname will always be /app, even though /app/index/view.js is the view that's being rendered.

I was wondering if it's possible to get the dirname of the current view rather than the base view. I know that I can add something like templatePath to each view that the base view will read, but I'm trying to avoid having to do that.




Aucun commentaire:

Enregistrer un commentaire