koltyakov / generator-sppp

šŸ¾ SP Pull-n-Push - Yeoman generator for SharePoint client-side applications
MIT License
64 stars 12 forks source link

Pass the data object to .hbs webpart to be compiled #11

Closed ademir85 closed 6 years ago

ademir85 commented 6 years ago

Hi,

I'm barely new in this topic, so any help would be appreciated.

Is it possible to pass the data to the .hbs template before compiling? For example, I'm aiming to execute an ajax request to fetch some data and render it in a webpart once the request is executed. Is this supported out of the box in the generator-sppp project or I need to manualy execute compiling of .hbs template?

Thanks in advance. Ademir

koltyakov commented 6 years ago

Hey Ademir,

Thanks for posting the question!

Currently only a limited amount of props is available for usage in .hbs: some settings layer and custom props from app.json.

I usually inject publish path and branding variables.

There is no easy way to inject async data at the moment.

Iā€™ll take a look to provide some sort of preexecutable task before build/watch tasks for extending data context.

Yet, canā€™t name any ETA. This weekend and next week going to be very rought on my end.

ademir85 commented 6 years ago

Hi Andrew,

Thanks for quick response. I really appreciate it.

Ok. In the mean time I'll try to find some solution that could help.

Thanks, Ademir

koltyakov commented 6 years ago

@ademir85,

I actually managed to add the requested functionality. Please check a newly published version.

With new version ./build/tasks/customDataLoader.js file should be created. Inside the file, there should be an export of a function which should return a promise with a custom data object.

E.g.:

//@ts-check

const path = require('path');
const pnp = require('sp-pnp-js');
const PnpNode = require('sp-pnp-node').PnpNode;

module.exports = (buildSettings, configs) => {
  // Function should return a promise with custom data object
  const privateConf = require(path.join(process.cwd(), buildSettings.privateConf));
  new PnpNode({
    siteUrl: privateConf.siteUrl,
    authOptions: {
      ...privateConf
    }
  }).init();
  return pnp.sp.web.get();
};

then in .hbs custom data is accessible within customData object.

In existing sppp projects:

Please let me know if it works for you?

koltyakov commented 6 years ago

Closing the issue. Please reopen if any questions still remain.