A
A
Alexander Evgenievich2015-05-15 15:27:32
JavaScript
Alexander Evgenievich, 2015-05-15 15:27:32

Correct approach to organizing data output from the server in Angular?

Hello.
An Angular app has a Brief model:

angular.module('Application').factory('Model', function(){
    function Model(){
      this.id = '';
      this.title = '';
      this.description = '';
      this.coverImage = '';
      this.status = '_public';
      this.skills = [];
      this.category = [];
      this.budgetFrom = 1;
      this.budgetTo = 1;
      this.firstReview = new Date();
      this.deadline = new Date();
    }

    return Model;
  });

Data is written to the model, which is then sent to the server in the same form. The server is recording.
There is also an opportunity on the site to view a list of all the objects that you have created, there is a separate page for this. At the moment, the list of objects is displayed using php&twig. Of course, I would like to output with angular, however, in order to form with angular, JSON objects with properties corresponding to Model must come from the server.
However, there are some parameters that are specific and calculated for a particular user. Given these parameters, the output template of each object changes (for example, one object will have a button, the other will not) because of this Model != ResponseModel (this is an object from the server), since the ResponseModel will have the properties "canFinish", "canContest"
  • If you enter separate functions to calculate these parameters on the client, then there will be additional requests to the server for each object, which is not good.
  • You can also create a second ModelResponse model, but this approach introduces restrictions on the use of services that can work with the Model and will have to be duplicated.
  • There is an option not to use the Model, but to work directly with objects from the server, send them to the template, but then it will not be possible to work with services that use the Model, and you will have to write additional duplicate code, or transformers.

I hope I wrote clearly. Please advise the most suitable option.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gavrilov, 2015-05-15
@banderos120

Extend the Model with "canFinish", "canContest" properties and don't sweat your brain

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question