Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request: transforms #12

Open
jonschlinkert opened this issue Dec 18, 2014 · 5 comments
Open

request: transforms #12

jonschlinkert opened this issue Dec 18, 2014 · 5 comments
Labels

Comments

@jonschlinkert
Copy link
Owner

I think we should consider changing transforms to expose cache.data as a data parameter by default.

Example

Let's say you want to add a year property to the context. Currently it might look like this:

template.transform('year', function(app) {
  var data = app.cache.data;

  if (!data.year) {
    app.data('year', new Date().getFullYear());
  }
};

With the suggested changes, it might look more like this:

template.transform('year', function(data) {
  if (!data.year) {
    data.year = new Date().getFullYear();
  }
};

and possibly use an async signature like middleware, eg.

template.transform('year', function(data, next) {
  if (!data.year) {
    data.year = new Date().getFullYear();
  }
  next(null, data);
};

Thoughts @doowb?

@doowb
Copy link
Collaborator

doowb commented Dec 18, 2014

I think adding data is fine... or binding it so it's this.data. Also, we already pass in the additional arguments to transform to the function, so we could update that to do something like:

Template.prototype.transform = function (name, fn /*, ... */) {
  var args = slice(arguments, 2);
  var context = {
    data: this.cache.data,
    name: name
  };
  this._.transforms[name] = fn;
  return fn.apply(context, args);
};

Now the user can make the transform async just by doing:

var yearTransform = function (next) {
  if (!this.data.year) {
    this.data.year = new Date().getFullYear();
  }
  next(null, this.data);
};
template.transform('year', yearTransform, function (err, data) {
  // validate data or something
});

That is unless you're expecting to do something else with the data object passed back through next. Then we would have to explicitly handle all that.

@jonschlinkert
Copy link
Owner Author

no this is good, it's similar to how you did the transforms on your example project isn't it?

@doowb
Copy link
Collaborator

doowb commented Feb 22, 2015

Yeah, this was similar, but I'm not really using data. I'm adding properties to the cache object with app.set('foo', 'bar') so I can use those in helpers instead of having that information merged into the context.

I think if we updated transforms to be more like the binding in helpers, then it'll provide more flexibility and the arguments will make more sense:

Template.prototype.transform = function (name, fn /*, ... */) {
  var args = slice(arguments, 2);
  var context = {
    app: this,
    data: this.cache.data,
    options: this.options,
    name: name
  };
  this._.transforms[name] = fn;
  return fn.apply(context, args);
};

@jonschlinkert
Copy link
Owner Author

can use those in helpers

how are you using that data in helpers? just this.app.foo? more and more I'm thinking we need have a separate object for storing each object separately so that the user can do any and all merging if necessary. seems like it would untangle a lot of complicated logic. it's not easy to do the context stuff, which is why it seemed necessary to build it in, but now I'm thinking it's the opposite. thoughts?

@doowb
Copy link
Collaborator

doowb commented Feb 24, 2015

I use it with the get/set methods on the app.

var foo = this.app.get('foo');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants