Frage

The following code sample (taken from AngularJS ui-router wiki) illustrates my problem.

angular.module('myApp', ['ui.router', 'ui.router.stateHelper'])
  .config(function(stateHelperProvider){
    stateHelperProvider.setNestedState({
      name: 'root',
      templateUrl: 'root.html',
      children: [
        {
          name: 'contacts',
          templateUrl: 'contacts.html',
          children: [
            {
              name: 'list',
              templateUrl: 'contacts.list.html'
            }
          ]
        },
        {
          name: 'products',
          templateUrl: 'products.html',
          children: [
            {
              name: 'list',
              templateUrl: 'products.list.html'
            }
          ]
        }
      ]
    });
  });

Specifically, these lines:

            }
          ]
        }
      ]
    });
  });

I find this very ugly. Is there a way to avoid these?

NOTE: I realize that CoffeeScript can help somewhat, but I am mainly interested in what can be done with Javascript.

War es hilfreich?

Lösung

There's always divide and conquer:

function appropriateNameHere(stateHelperProvider) {
    var contacts, products, state;

    contacts = {
        name: 'contacts',
        templateUrl: 'contacts.html',
        children: [{
            name: 'list',
            templateUrl: 'contacts.list.html'
        }]
    };
    products = {
        name: 'products',
        templateUrl: 'products.html',
        children: [{
            name: 'list',
            templateUrl: 'products.list.html'
        }]
    };
    state = {
        name: 'root',
        templateUrl: 'root.html',
        children: [contacts, products]
    };
    stateHelperProvider.setNestedState(state);
}
angular.module(
    'myApp',
    ['ui.router', 'ui.router.stateHelper']
  ).config(appropriateNameHere);

Where you draw the line there is almost totally subjective/opinion-based. Some people will be just fine with your code as-is, others will feel it needs to be made more granular than I did above. But that's the fundamental technique you'd use when applying your opinion. :-)

Andere Tipps

You could create function, instead of using anonymous ones. You could also use variables instead of passing data directly.

Is there a way to avoid these?

You could try Coffeescript, where you would write

angular.module("myApp", [
  "ui.router"
  "ui.router.stateHelper"
]).config (stateHelperProvider) ->
  stateHelperProvider.setNestedState
    name: "root"
    templateUrl: "root.html"
    children: [
      {
        name: "contacts"
        templateUrl: "contacts.html"
        children: [
          name: "list"
          templateUrl: "contacts.list.html"
        ]
      }
      {
        name: "products"
        templateUrl: "products.html"
        children: [
          name: "list"
          templateUrl: "products.list.html"
        ]
      }
    ]

But no, in general you cannot avoid them.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top