by cia_plant · 13y ago · 0 comments
<span ng-switch="foo">
<span ng-switch-when="bar">...</span>
<span ng-switch-default>...</span>
</span>
Is in some way easier or better than this: if(foo == bar) { ... } else { ... }
It also has this weird retro stuff like dynamic scopes and reactive programming. It's also really slow, because it implements reactive programming with dirty checking.Still not sure what problem it's trying to solve.
Its one of the standard ways of developing WINDOWS applications. One example of declarative UI is HTML and CSS. You never write any "HOW TO LOGIC" when you are "printing" buttons, textboxs etc. on browsers. You just say <button> and there you go, you've a button there. You say <button class="red"> and lo and behold your button turns red. In that way you don't care "how" to print UI elements, you just focus WHAT YOU WANT TO DO rather HOW YOU WILL DO IT.
Declarative UI makes overall application highly testable and maintainable. You don't think how to "print" things on screen (write tons of binding code). Writing tests for the UI specific code is not only hard but messy. Without strong test-ability you can't scale your app without pains.
There is a reason why Google adopted this approach because world has learned the "best way" for building UIs. Google has probably half a decade of experience of building Google Web Toolkit. Right now Google Web Toolkit GWT has lot of similar concepts to AngularJS. Microsoft has probably 20 years experience of building UI framworks. Right now WP8 and WPF are all declarative UIs. In near future you'll see almost every UI framework across all devices technology will employ more or less the same concepts as AngularJS has now.
AngularJS is solving the testability & maintainability problem of large scale Javascript apps.
1) The first example is a template/view. It is reactively updated automatically when the values referenced are updated. It maps to markup on the page. The second example is a basic control structure that does nothing.
2)
<span ng-bind="foo == bar ? '...' : '...'"></span>However, it does fit in with a long line of tools whose premise is "Normal programming with normal control flow is an insufficient tool for the unique problems of ${DOMAIN}. We need to create a new programming model with its own syntax and exotic control flow to make things easier."
The appearance of the "if-statement-in-markup" (or "if-statement-in-flowchart-diagrams", etc.) is to me a sure sign of a certain kind of misguided system.
Which is a hard and important problem; nothing trivial.
<span>{{ someValue }}</span>
And in the controller JS file do switch (foo) {
case 'bar':
scope.someValue = 'hello';
break;
...On the performance - it's going to be slower than doing everything in specifically optimised code, but it's almost certainly not going to be a problem. My project has a million+ objects in js once the interface loads and I have watches on a big chunk of that data and the interface is still snappy. Granted, there are bits where I have to be careful to make it snappy, but that's life. Development time is so much faster I wouldn't make the tradeoff for the world now.
Other frameworks also provide two way binding.
> better than in JS
Handlebars is much more like 'programming _with_ HTML' than 'programming in JS'
Furthermore, those who don't like annotation aren't writing JavaScript views, they're writing HTML templates and annotating them with some JS, which means the parent offers a false dichotomy.
Please reconsider whatever it is you think you're doing, it's seriously not helping anyone.