0

We're nearing the completion of working on an embedible JavaScript widget which has been built with AngularJS, to date everything it working fine, however we're now at the point where we need to allow the site owner to inject config settings into the JavaScript such as:

  • company_id
  • user
    • name
    • email
    • id
  • default overrides

In a previous iteration of our widget we used raw JavaScript, and were able to add settings like this:

Widget = window.Widget || [];
(function () {
    var i, e;
    i = document.createElement("script"),
        i.type = 'text/javascript';
    i.async = 1,
        i.src = "https://path/to.js",
        e = document.getElementsByTagName("script")[0],
        e.parentNode.insertBefore(i, e);
})();
Widget.push(['set', {
    'account_id': 'konami',
}]);

Inside our JavaScript, we simply had a window.Widget object which we overwrote with their settings and used wherever we needed.

However I'm at a loss as to how / where to receive this information and use it from inside our angular app, would appreciate any help on the best way to add in settings / configuration from when the JavaScript is loaded, and be able to use this in our services / controllers.

2
  • Can you clarify your previous solution a bit more? It's not clear where the data (company_id, user, etc.) was.
    – rakslice
    Sep 29, 2014 at 4:45
  • just on the website that was going to host the widget, they were what was to be injected into the script
    – duellsy
    Sep 29, 2014 at 5:13

1 Answer 1

0

I ended up just adding the settings I wanted directly on an object on the window, then used that inside my angular project. ala

_widget = window._widget || [];
_widget.company_id = '1234';
_widget.user = {name: 'Chris'};

Definitely not the pretty way I was hoping for, but it gets the job done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.