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.
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. :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With