webroo / dummy-json

Generates random dummy JSON data in Node.js
MIT License
380 stars 61 forks source link

How to create custom synched helpers? #11

Closed jayantbh closed 5 years ago

jayantbh commented 8 years ago

What if I want to create a helper that uses the same set of data as the synchronized helpers without accidentally creating a new set of data when reusing one of the synched helpers?

webroo commented 8 years ago

Unfortunately the code that syncs helpers is wrapped up inside dummy-json and not publicly accessible via it's api. I would like to change this in a future update but I haven't decided on the best way doing it yet.

I have two approaches that I've been thinking about:

1) Allow a flag to be sent to a helper to prevent it syncing, eg:

var myHelpers = {
  fullname: function(options) {
    options.sync = false; // Turn off synced helpers
    return dummyjson.helpers.firstName(options) + ' ' + dummyjson.helpers.lastName(options);
  }
};

2) Expose the previously generated values for every helper on the Handlebars root data:

var myHelpers = {
  fullname: function(options) {
    // options.data.root.__prev will contain the previously generated value of every helper
    return options.data.root.__prev.firstName + options.data.root.__prev.lastName;
  }
};

(Helpers always receive an options object so it would be accessible to every custom helper you write)

I need to try both these approaches and see how they work. I'll leave this issue open in the mean time.

lnfnunes commented 8 years ago

+1 (flag option) ...but with positive instead of negative "sync = true" when I want this option.

chuSS commented 7 years ago

it case for my wants? var myHelpers = { passwdPlain: function() { return randomPassword(); }, passwdSecret: function(passwdPlain) { return sha256(passwdPlain); } }