3

I've been looking at frameworks to help me write better structured JS code and came across Backbone. It seems to have lots of functionality for creating classes with public and private properties and members, inheritance, etc.

Angular JS seems to do a load of completely different stuff like automatic binding of markup to model and lots of clever trickery for templating, filtering, sorting etc.

My question is not which should I choose, but is it sensible to use both? AngularJS looks really cool, but unless I'm missing something then it doesn't seem to provide any OO stuff for you to work with. Does this make sense?

jonhobbs
  • 26,684
  • 35
  • 115
  • 170
  • 1
    I find my self in the same dilema, I think this is a very respetable question. Angular provider a lot of magic for _binding_ stuff but I'm missing some kind of _inheritance_ and _collections abstraction_ as Backbone provides. – fguillen Mar 28 '14 at 10:33

3 Answers3

3

I'm not sure that pairing Angular with another framework would really make a lot of sense, it's quite complete as it is.

Backbone on the other hand is more composable, for instance you might take a look at the Knockback project, which make Backbone and KnockoutJS work together quite nicely. Knockout takes a MVVM approach which is relatively close to what you'll find in Angular.

Manu Clementz
  • 1,807
  • 12
  • 19
  • 1
    Backbone seems to let you do stuff like this... "var PrivateNote = Note.extend({initialize: function() { ... }});" , which is what I was getting at I think. Can't see anything in Angular which is similar. – jonhobbs Aug 06 '12 at 10:15
  • Hi jonhobbs, just remember.. this is Javascript. You don't have to write OO, it's prototypal language :-) – Andrew Joslin Aug 06 '12 at 11:48
  • That is true, OO isn't necessarily a requirement, but I'm about to start creating a big admin system with lots of JS and I want it to be as organized as possible. – jonhobbs Aug 06 '12 at 13:08
  • With its controllers and components system, Angular provides a really nice organization system. On the other hand if you'd rather use a more "traditional" MVC framework, maybe you could take a look at some others. [This post](http://blog.stevensanderson.com/2012/08/01/rich-javascript-applications-the-seven-frameworks-throne-of-js-2012/) for instance offers a comparison of the biggest contenders in the field. – Manu Clementz Aug 06 '12 at 13:27
  • I'm writing a really big admin panel in Angular right now too, and I'm very happy I don't have to do it in OO. I don't have to write a little model for everything, and can just use prototypal inheritance if I ever need it. Angular makes it super easy to write modular code, too, with built in dependency injection and seperation of directives/services/controllers. – Andrew Joslin Aug 06 '12 at 14:37
  • I have written a medium sized web app in AngularJS ( around 2k lines now perhaps ). If you follow the structure provided by Angular-seed you should be pretty much setup good. Unless you are writing really large directives / controllers ( which should not happen, if you are are doing it right ! ) you will need very less code to get through your whole project. – ganaraj Aug 06 '12 at 16:19
2

If you need to write OO for your data (aka Models), just use a library like Base.js. It gives you nice OO syntax, inheritance, with methods like extend. The good part is that it keeps the getter syntax like myObject.var so it plays nicely with Angular, contrarily to Backbone where you need to use a getter myObject.get('var'), so data bindings works just fine.

To understand the difference and the reason between those syntaxes and philosophy, read one of the Angular author's answer here in SO.

Community
  • 1
  • 1
DjebbZ
  • 1,594
  • 1
  • 18
  • 34
1

AngularJS purposely does not impose any particular inheritance style, so you're free to do whatever you want. If you like Backbone.js's inheritance model, you can use underscore (which is included in Backbone.js) to provide the .extend helper.

btford
  • 5,631
  • 2
  • 29
  • 26
  • 3
    Backbone.extend is not the same thing as _.extend. _.extends just adds properties on an object, while Backbone.extend generates subclasses. See the Helper section in http://backbonejs.org/docs/backbone.html#section-189 – Bob Fanger Apr 28 '13 at 17:55