2

Say I've got a model Kitty with two required fields, name and favoriteSnack. I want to let people edit Kitties in a view with a form that looks like:

Edit Kitty
Name: [______________]
Favorite snack: [__________________]
[Submit]

I'm stuck -- the boring days of GET and POST seemed to be easier. So what's a good pattern for setting this up with AngularJS? Specifically,

  • How should I set up my URLs and methods? Does ngResource do this automatically? I'm guessing,
    • GET /kitties - list all kitties
    • GET /kitties/:id - get a single kitten
    • POST /kitties - create a new kitty
    • PUT /kitties/:id - update a kitty
    • DELETE /kitties/:id - delete a kitty
  • On submit, should I validate the model client-side or server-side?
  • If server-side, how do I handle non-200 responses from the REST API that I've hooked my model up to?
  • How should I propagate errors back to the user?
  • Can I do validate-as-you-type?

Please forgive me if I've missed something obvious.

1 Answer 1

4
  • How should I set up my URLs and methods? Does ngResource do this automatically?

Yes it does, and yes you should use ngResource.

  • On submit, should I validate the model client-side or server-side?

You should always validate anything sent to the server on the server, whether you do client-side validation or not. In Angular, however, you don't need to do any validation on submit, as validation can happen as you type (your last question)

  • If server-side, how do I handle non-200 responses from the REST API that I've hooked my model up to?

You can handle them in the error argument of your ngResource methods. See the Returns section in the documentation.

  • How should I propagate errors back to the user?

Do it the Angular way and simply set a property on your model in the event of an error. This property should be rendered somewhere on your UI. If you code it right, you can pretty much guarantee that anyone using your application the right way will have seen the client-side errors, so I wouldn't put too much effort in field-specific errors from the server.

  • Can I do validate-as-you-type?

Yes, using all of the input attributes.

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

1 Comment

Thanks for this! AngularJS has a ways to go on its documentation before this would have been obvious.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.