-
-
Save teamon/7678184 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| db = { | |
| person: { | |
| save: function(obj){ | |
| if(obj.id){ | |
| return $http.post("/person", obj).then(function(res){ | |
| var data = angular.extend({}, obj); // create copy of `obj` + `id` field | |
| data.id = res.data.id; | |
| return data; | |
| }) | |
| } else { | |
| return $http.put("/person", obj) | |
| } | |
| } | |
| } | |
| } | |
| var person = {firstName: 'John', lastName: 'Smith'} | |
| db.person.save(person).then(...) | |
| person.firstName = "Michael" | |
| db.person.save(person).then(...) |
Also PUT should be something like $http.put("/person/" + obj.id, obj)
Author
- condition - sure, it should be opposite
Promise#thentransforms promise value so nextthencall will receive modified object- PUT path with id - sure :)
Ad 2. But it doesn't mean your person object will be modified, right? You would have to modify it in then callback:
.then(function(success){
person = success.data;
});
Do I miss anything?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think the condition is opposite:
PUTshould be sent whenobj.idis defined.Also I guess you assign
res.data.idtodata.idwhich is the copy of originalpersonobject, so in the seconddb.person.save(person).then(...)callperson.idwill be still undefined. But I might be wrong.