|
1 | 1 | /**
|
2 | 2 | * @author Jason Dobry <[email protected]>
|
3 | 3 | * @file angular-data.js
|
4 |
| -* @version 0.10.3 - Homepage <http://angular-data.pseudobry.com/> |
| 4 | +* @version 0.10.4 - Homepage <http://angular-data.pseudobry.com/> |
5 | 5 | * @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/>
|
6 | 6 | * @license MIT <https://github.com/jmdobry/angular-data/blob/master/LICENSE>
|
7 | 7 | *
|
@@ -2064,6 +2064,7 @@ var errorPrefix = 'DS.create(resourceName, attrs[, options]): ';
|
2064 | 2064 | * @param {object=} options Configuration options. Properties:
|
2065 | 2065 | *
|
2066 | 2066 | * - `{boolean=}` - `cacheResponse` - Inject the data returned by the server into the data store. Default: `true`.
|
| 2067 | + * - `{boolean=}` - `upsert` - If `attrs` already contains a primary key, then attempt to call `DS.update` instead. Default: `true`. |
2067 | 2068 | *
|
2068 | 2069 | * @returns {Promise} Promise produced by the `$q` service.
|
2069 | 2070 | *
|
@@ -2096,37 +2097,45 @@ function create(resourceName, attrs, options) {
|
2096 | 2097 | options.cacheResponse = true;
|
2097 | 2098 | }
|
2098 | 2099 |
|
2099 |
| - promise = promise |
2100 |
| - .then(function (attrs) { |
2101 |
| - return _this.$q.promisify(definition.beforeValidate)(resourceName, attrs); |
2102 |
| - }) |
2103 |
| - .then(function (attrs) { |
2104 |
| - return _this.$q.promisify(definition.validate)(resourceName, attrs); |
2105 |
| - }) |
2106 |
| - .then(function (attrs) { |
2107 |
| - return _this.$q.promisify(definition.afterValidate)(resourceName, attrs); |
2108 |
| - }) |
2109 |
| - .then(function (attrs) { |
2110 |
| - return _this.$q.promisify(definition.beforeCreate)(resourceName, attrs); |
2111 |
| - }) |
2112 |
| - .then(function (attrs) { |
2113 |
| - return _this.adapters[options.adapter || definition.defaultAdapter].create(definition, definition.serialize(resourceName, attrs), options); |
2114 |
| - }) |
2115 |
| - .then(function (res) { |
2116 |
| - return _this.$q.promisify(definition.afterCreate)(resourceName, definition.deserialize(resourceName, res)); |
2117 |
| - }) |
2118 |
| - .then(function (data) { |
2119 |
| - if (options.cacheResponse) { |
2120 |
| - var created = _this.inject(definition.name, data); |
2121 |
| - var id = created[definition.idAttribute]; |
2122 |
| - resource.completedQueries[id] = new Date().getTime(); |
2123 |
| - resource.previousAttributes[id] = _this.utils.deepMixIn({}, created); |
2124 |
| - resource.saved[id] = _this.utils.updateTimestamp(resource.saved[id]); |
2125 |
| - return _this.get(definition.name, id); |
2126 |
| - } else { |
2127 |
| - return data; |
2128 |
| - } |
2129 |
| - }); |
| 2100 | + if (!('upsert' in options)) { |
| 2101 | + options.upsert = true; |
| 2102 | + } |
| 2103 | + |
| 2104 | + if (options.upsert && attrs[definition.idAttribute]) { |
| 2105 | + promise = this.update(resourceName, attrs[definition.idAttribute], attrs, options); |
| 2106 | + } else { |
| 2107 | + promise = promise |
| 2108 | + .then(function (attrs) { |
| 2109 | + return _this.$q.promisify(definition.beforeValidate)(resourceName, attrs); |
| 2110 | + }) |
| 2111 | + .then(function (attrs) { |
| 2112 | + return _this.$q.promisify(definition.validate)(resourceName, attrs); |
| 2113 | + }) |
| 2114 | + .then(function (attrs) { |
| 2115 | + return _this.$q.promisify(definition.afterValidate)(resourceName, attrs); |
| 2116 | + }) |
| 2117 | + .then(function (attrs) { |
| 2118 | + return _this.$q.promisify(definition.beforeCreate)(resourceName, attrs); |
| 2119 | + }) |
| 2120 | + .then(function (attrs) { |
| 2121 | + return _this.adapters[options.adapter || definition.defaultAdapter].create(definition, definition.serialize(resourceName, attrs), options); |
| 2122 | + }) |
| 2123 | + .then(function (res) { |
| 2124 | + return _this.$q.promisify(definition.afterCreate)(resourceName, definition.deserialize(resourceName, res)); |
| 2125 | + }) |
| 2126 | + .then(function (data) { |
| 2127 | + if (options.cacheResponse) { |
| 2128 | + var created = _this.inject(definition.name, data); |
| 2129 | + var id = created[definition.idAttribute]; |
| 2130 | + resource.completedQueries[id] = new Date().getTime(); |
| 2131 | + resource.previousAttributes[id] = _this.utils.deepMixIn({}, created); |
| 2132 | + resource.saved[id] = _this.utils.updateTimestamp(resource.saved[id]); |
| 2133 | + return _this.get(definition.name, id); |
| 2134 | + } else { |
| 2135 | + return data; |
| 2136 | + } |
| 2137 | + }); |
| 2138 | + } |
2130 | 2139 |
|
2131 | 2140 | deferred.resolve(attrs);
|
2132 | 2141 | } catch (err) {
|
@@ -2274,6 +2283,7 @@ var errorPrefix = 'DS.destroyAll(resourceName, params[, options]): ';
|
2274 | 2283 | * - `{string|array=}` - `orderBy` - OrderBy clause.
|
2275 | 2284 | *
|
2276 | 2285 | * @param {object=} options Optional configuration. Properties:
|
| 2286 | + * |
2277 | 2287 | * - `{boolean=}` - `bypassCache` - Bypass the cache. Default: `false`.
|
2278 | 2288 | *
|
2279 | 2289 | * @returns {Promise} Promise produced by the `$q` service.
|
@@ -4763,8 +4773,10 @@ var errorPrefix = 'DS.filter(resourceName[, params][, options]): ';
|
4763 | 4773 | * - `{string|array=}` - `orderBy` - OrderBy clause.
|
4764 | 4774 | *
|
4765 | 4775 | * @param {object=} options Optional configuration. Properties:
|
| 4776 | + * |
4766 | 4777 | * - `{boolean=}` - `loadFromServer` - Send the query to server if it has not been sent yet. Default: `false`.
|
4767 | 4778 | * - `{boolean=}` - `allowSimpleWhere` - Treat top-level fields on the `params` argument as simple "where" equality clauses. Default: `true`.
|
| 4779 | + * |
4768 | 4780 | * @returns {array} The filtered collection of items of the type specified by `resourceName`.
|
4769 | 4781 | */
|
4770 | 4782 | function filter(resourceName, params, options) {
|
@@ -5254,7 +5266,7 @@ function _injectRelations(definition, injected) {
|
5254 | 5266 | *
|
5255 | 5267 | * @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
|
5256 | 5268 | * @param {object|array} attrs The item or collection of items to inject into the data store.
|
5257 |
| - * @param {object=} options Optional configuration. Properties: |
| 5269 | + * @param {object=} options Optional configuration. |
5258 | 5270 | * @returns {object|array} A reference to the item that was injected into the data store or an array of references to
|
5259 | 5271 | * the items that were injected into the data store.
|
5260 | 5272 | */
|
|
0 commit comments