1

As a new AngularJS developer (coming from PHP+Laravel world) I'm facing some troubles designing the architecture of my new app. Which is the best way to implement a CRUD app where entities are used more than once along the app?

For example: we have the entities 'document' and 'project'. Documents can be listed and viewed alone, but also can be attached to projects. Inside the project detail view I would like to include the attached documents, using the same template used when listing the documents alone. This widget should have its own controller and methods, since its need to make some API calls and apply some business logic; and receive the parent project data in some way.

What should I use for document listing? A directive, a ng-include or some other?

3 Answers 3

1

You should use module to use it as reusing component. https://docs.angularjs.org/guide/module

Sign up to request clarification or add additional context in comments.

Comments

1

i'm utilizing angular module and factory like this:

app.js

'use strict';
/* App Module */

var app = angular.module('my-app', [

    'my-models',

]);

my-models.js

var myModels = angular.module('my-models', []);


myModels.factory('DocumentsModel', function ($http)
{
    var DocumentsModel = function ()
    {
    };

    DocumentsModel.get_documents = function (page, results_per_page)
    {

        var data = {
            page: page,
            results_per_page: results_per_page
        };

        var json = angular.toJson(data);

        return $http.post('/api/documents', json);
    };

    DocumentsModel.set_document_state = function (document_id, document_state_id)
        {
            var json = angular.toJson(
                {
                    'document_state': document_state_id
                }
            );

            return $http.post('api/document/'+document_id', json);
        };

    return DocumentsModel;
});

using angular dependency injection mechanism, you can re-use this logic in multiple controllers by adding DocumentsModel to the controller function as parameter:

documents-ctrl.js

var app = angular.module('my-app');

var controller = app.controller("DocumentsCtrl",
    function ($scope, DocumentsModel)
    {

        DocumentsModel.get_documents()
            .success(function(data){
                $scope.documents = data.documents;
            });

    });

in addition, you cad define one for your 'project' entity.


Edit:

Javier commented:

enter image description here

assuming your documents response is

[{name: ... , size: ... , last_modified: ... }, {name: ... , size: ... , last_modified: ... }, ...]

you can utilize ng-repeat like this:

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Size</th>
            <th>Last Modified</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="document in documents">
            <td>{{ document.name }}</td>
            <td>{{ document.size/1024 | number:4 }} MB</td>
            <td>{{ document.last_modified | date:'yyyy-MM-dd HH:mm:ss' }}</td>
        </tr>
    </tbody>

</table>

3 Comments

And what should I use to render the document listing inside a project template?
@JavierMarín, added example with ng-repeat
I was thinking of some 'document widget' that I can use alone when listing documents, or inside a 'project' template, for list all project documents.
0

Just add it as a dependency to your own module. Like

angular.module('test', []);

angular.module('test2', ['test']);

You might want to take a look at Yeoman

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.