Skip to content

Use ngResource for breeds#2

Open
stevecass wants to merge 5 commits intofactor-modelfrom
use-dollar-resource
Open

Use ngResource for breeds#2
stevecass wants to merge 5 commits intofactor-modelfrom
use-dollar-resource

Conversation

@stevecass
Copy link
Contributor

This uses ngResource to make the Breed model act a bit like an ActiveRecord model resource mapped to an API. ngResource handles the mapping to a standard Rest API and provides instance methods like $save along providing the equivalent of ActiveRecord "class" methods. So

ngResource AR
Breed.query Model.all
Breed.get(:id) Model.find(:id)
breed.$save model_instance.save (if new_record?)
breed.$update model_instance.save (if not new_record?)

One potential wrinkle working with rails is that the default save doesn't distinguish between new and saved records. So we can add an override.

  var Breed = $resource('https://stark-harbor-5038.herokuapp.com/breeds/:id', { id: '@id' }, {
    create: {
      method: 'PATCH'
    }
    update: {
      method: 'PATCH'
    }
  });

So our breed instances have $update and $create attached to them. To keep with the AR program we're also implementing a save (no $ prefix) method on the instance to call either create or update depending on the instance state.

https://docs.angularjs.org/api/ngResource/service/$resource

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant