0

I am very new to angular and this one is striking in my head a lot. So scenario is : Suppose angular http returns me model containing array of object like:

[{name:"Ankur",lastName:"aggarwal",updation_date:"23-08-2014"},{name:"xyz",lastName:"abc",updation_date:"29-08-2013"}]

Out of this updation_date is not required but coming for some reason. So is it right to update the array with third object without creation date like {name:"def",lastName:"jbc"} . Is it a good practice or array object model should be consistent?

Also what should be the approach? Update the model array first so binding take place instantly, then send it to the server or send it to server and get the updated object? Might be basic one but very new to angular and JMVC.

2 Answers 2

1

Is it a good practice or array object model should be consistent?

It depends , if backend expects all array entries to contain updation_date then you have no choice and are forced to add some sensible default value. However, if possible then avoid sending too much unnecessary data from backend since it impacts application performance(like data transfer, adding unnecessary logic to generate sensible default values, etc.)

Update the model array first so binding take place instantly, then send it to the server or send it to server and get the updated object?

If the nature of your application permits reverting model value when save is unsuccessful then just go ahead with

0.Perform data validation, and make sure valid data is supplied to the backend.

1.Update model.

2.Send data to backend

3.If something bad happens then execute error handling depending on app needs

However if presenting consistent value in the GUI is uttermost importance(e.g. finance applications) then

0.Perform data validation, and make sure valid data is supplied to the backend.

1.Show some message to user like "saving"

2.Perform ajax request

3.If successful, update model, else execute error handling depending on app needs

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

Comments

1

It depend on your error handling.

As saving on the server-side might be not successful, you should take it into consideration.

My approach is to

  1. Update angular object immediately
  2. Then send AJAX request to server and
  3. Wait for response. If error happen during server save, you shoulde:
    • revert values,
    • repeat AJAX
    • show information to user.

2 Comments

thanks for the response. What about adding different objects in same array model. Is it a helpful approach or should be avoided?
For me it is not important if you have consistent data structure if you don't use it at all. But you should think twice about future use of this data, so you can minimize server requests size by removing this information (1/3 of all message)

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.