1
votes

I'm creating a Marionette web app using requirejs. I override the maronette view renderer to support my custom template objects like this:

Marionette.Renderer.render = function(template, data){
   return template.render(data);
};

This configuration is global to all the views. My problem is: Where is the best place to put this code(and similar config like this - example overridden 'sync' function in backbone) maintaining the modularity obtained via requirejs?

The options I could think of are:

  • Simply put it in the app or main file - seems like a quick n dirty way
  • Put all the custom config for all libraries in a config file and requiring it.
  • Make a file like MarionetteConfig or BackboneConfig and put the config specific to that library in that file and require that file.

What is the better way to do it?

Thanks..

1
I put it in the main file but probably would be better to require an external config file.Puigcerber
@Puigcerber : Thanks for the suggestion, do you recommend a single config file for those config stuff for all the libraries or one for each?Jithesh
Probably one for library putting all of them in some kind of folder like utils or helpers. You could then add all these files as dependencies in your shim configuration.Puigcerber

1 Answers

0
votes

Use require.config to provide configurations to your modules.

What you are asking for is not a configuration issue. Its more about how to extend modules.

Extending libraries as Marionette or Backbone should be done by specifying a custom module. I suggest to write one module for each library.

define('myBackbone', ['vendor/backbone', 'vendor/underscore'], function(Backbone, _) {
  Backbone.View.prototype.render = function() {
    console.log('My Custom view');
  }
  // Or doing it the Backbone/Underscore way
  //_.extend(Backbone.View.prototype, {
  //  render : function() {
  //    console.log('My Custom view')
  //  }
  //});
  return Backbone;
});

Then use myBackbone in your modules.

define('myModule', ['myBackbone'], function(Backbone) {
  return Backbone.View.extend({...});
});

Btw. Instead of extending the libraries with your application logic, you could also create an abstract view and extend your views thereof.