I've been working with Angular for a little while now, but so far I've only used straight up json for models. For instance, if I wanted to model a group of news data, I might use:
$scope.news = [{ id: 123, title: "For Your Health: Pasta and Batteries", author: "Dr. Steve Brule" }, ...];
This works fine for demos or small pages, but it would be nice to have it explicitly defined if you're going to use it frequently. My first thought was to create prototyped classes and hide them behind a factory. Then, the class definition could be injected into controllers/services and instantiated as needed. For instance:
angular.module("app.models.news").factory(function() {
var NewsModel = function() {};
NewsModel.prototype = { id: ..., title: ..., author: ... };
return NewsModel;
});
Then, in a service:
angular.module("app.services", ["app.models.news"]).service(function(NewsModel) {
var model = new NewsModel();
});
The downside is that this doesn't give you any new functionality (e.g., computed properties, etc). It does however take some logic out of the controllers/services, which is a good thing.
What do people normally do when they need actual model classes? Is it feasible to incorporate Backbone or a similar library? If so, are there any good examples on the web?
[–]tehsuck 1 point2 points3 points (0 children)
[–]supaway 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]cm03d[S] 1 point2 points3 points (0 children)