UPDATED
Here is a way you can continue incorporate your Note
constructor into your $resource
service...
Add a new instance method to the object returned from the factory:
res.getNew = function(){
var newNote = new Note();
newNote.id = undefined; // new note doesn't have id set
newNote.checked = undefined; // don't need this data passed to server?
angular.extend(newNote, res);
return newNote;
}
This method modifies an instantiated Note
object (to remove ID and other data that doesn't need to be passed to the server when creating a new note) before merging it with the $resource
service and returning that object to the caller.
Call that method from your controller assigning its return value to a local variable. You can also modify its properties from the controller:
var note = NoteResource.getNew();
note.title = 'Eat a lot of junk food';
Call the save
method on the note
object, passing itself as the sole parameter:
note.save(note).$promise.then(function() {
$scope.notes = NoteResource.query();
$scope.newNotes = [];
});
Watch in dev tools and notice that this does result in a JSON payload being sent with the POST containing your note
properties (which your previous solution did not).
Hopefully this solution ticks off your box for enforcing a model without having to using a factory in an unintended manner.
Plunker Demo