angularjs add a model layer

Issue

I am using angular with rails backend and I am using $resource to interact backend API. so I have a User service which is a $resource instance

App.factory('User', ['$resource', function($resource) {
  return $resource('/users/:id',{id: '@id'});
}])

Now I can use User to interact with rails API. but the problem is all the actions in User end with a HTTP request to rails. There is scenario where I just want to create a instance of this model, but not interacting with rails. in the convention of rails, I need a

User.new

which return a user object, but NOT

User.create

which will save this object to server.

The idea case is I can do the following:

var user = User.new({attr1: value1, attr2:value2.....});// new a User instance with supplied value
var promise = User.create(user) // save this user instance to server

How this could be achieved?

Solution

as apneadiving pointed out, the resource instance do the trick!

var user = new User({ attr1: value}), user.$save

Answered By – fuyi

Answer Checked By – David Marino (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.