backbone.jsasp.net-core-2.0backbone-model

Is there a way to pass .Net Core 2 appsettings.json properties down to backbone components?


In my appsettings.json file, I have a flag that I can set to enable or disable a learning assignment called "Telescope", like this:

"Telescope": {
   "Enable": false
},

In my Startup.cs, I can successfully check to see if it is enabled or not:

if(Configuration.Telescope.Enable) {
   ... do something ...
}

But I'm not sure how to transfer that logic down to my Backbone components.

For example, I have this in a Backbone model:

this.telescopeArray = new TelescopeCollection();
this.telescopeArray.url = () => `/assignments/telescopes/location/${this.id}`;

And this, in an html template:

<a href="/assignments/telescopes/students/{{ ctx.locationId }}/">Start Your Learning Assignment</a>

As I said above, I can enable or disable "Telecope" in my appsettings.json file, and I can see that it's either enabled or disabled in the c# part of my app, but I'm not sure how to enable or disable the lines of code in the Backbone part of my app.

Is there a way to do this in Backbone?

Thanks!


Solution

  • I can think of following ways:

    1. Bootstrapping.
      When your app first loads, add some initial data in the DOM itself such as in a <meta> tag or as <script> etc. Later other parts of your app can query this, or better just query it on Backbone app initialization and set it on a model/state store for easy access later.
    2. REST API with a state store or backbone model
      Have a dedicated REST API that sends your application settings. You can retrieve this via a backbone model or any other state store that you like. You can do this when your application initializes, later other parts of your application can access this store to get the values
    3. A mix of both of the above - If this value can change on server side after loading the application, you can use bootstrapping for fast loading initial state and then the REST API to check the current state before performing any actions based on this setting