Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have an application that looks like such:

var app = angular.module('app', []);

app.Person = function(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
}

app.controller = function('myController', function($scope) {

    $scope.pageData = [];
    $scope.pageData.firstName = ['John'];
    $scope.pageData.lastName = ['Smith'];

    $scope.pageData.people = [];

    // just a demo. The point of this is just to show the controller creating Person objects 
    // assume that first name and last name will always be the same length;
    for (var i = 0; i < firstName.length; i++) {
        $scope.pageData.people.push(new Person($scope.pageData.firstName[i], $scope.pageData.lastName[i]);
    }
});

Is there a better way of structuring Person? Should it be in a factory or service?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

As Pierre already mentioned, you don't need to create new class for anemic model (like in backbone for example). But sometimes you want to go with rich domain model. Then you can use factory:

angular.module('app')
    .factory('Person', function () {

        function Person(firstName, lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        Person.prototype.doSomething = function () {};

        return Person;
    });

and inject your model constructor into controller (or service)

app.controller = function('myController', function($scope, Person) {

    $scope.pageData = [];
    $scope.pageData.firstName = ['John'];
    $scope.pageData.lastName = ['Smith'];

    $scope.pageData.people = [];

    // just a demo. The point of this is just to show the controller creating Person objects 
    // assume that first name and last name will always be the same length;
    for (var i = 0; i < firstName.length; i++) {
        $scope.pageData.people.push(new Person($scope.pageData.firstName[i], $scope.pageData.lastName[i]);
    }
});

Notice that you still have to instantiate new Person, you can use builder pattern to avoid that. Check this blog for more information: https://medium.com/opinionated-angularjs/angular-model-objects-with-javascript-classes-2e6a067c73bc

share|improve this answer

Because Javascript allows you to easily create objects without an explicit class, I usually find it unnecessary to define a class that doesn't have any functions. That is, there's no benefit to using new Person(x,y) over {firstName: x, lastName: y}. In fact, I would back up a step and have that kind of data in my model instead of separate arrays of first and last names:

$scope.pageData = [{firstName: 'John', lastName: 'Smith'} /* etc */];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.