dist/globals/ember-data.prod.js in ember-data-source-2.6.0.beta.2 vs dist/globals/ember-data.prod.js in ember-data-source-2.6.0.beta.3

- old
+ new

@@ -4,11 +4,11 @@ /*! * @overview Ember Data * @copyright Copyright 2011-2016 Tilde Inc. and contributors. * Portions Copyright 2011 LivingSocial Inc. * @license Licensed under MIT license (see license.js) - * @version 2.6.0-beta.2 + * @version 2.6.0-beta.3 */ var loader, define, requireModule, require, requirejs; (function(global) { @@ -8486,21 +8486,84 @@ return (0, _emberDataPrivateSystemPromiseProxies.promiseArray)((0, _emberDataPrivateSystemStoreFinders._query)(adapter, this, typeClass, query, array)); }, /** - This method delegates a query to the adapter. This is the one place where - adapter-level semantics are exposed to the application. - Exposing queries this way seems preferable to creating an abstract query - language for all server-side queries, and then require all adapters to - implement them. - This method returns a promise, which is resolved with a `RecordObject` - once the server returns. + This method makes a request for one record, where the `id` is not known + beforehand (if the `id` is known, use `findRecord` instead). + This method can be used when it is certain that the server will return a + single object for the primary data. + Let's assume our API provides an endpoint for the currently logged in user + via: + ``` + // GET /api/current_user + { + user: { + id: 1234, + username: 'admin' + } + } + ``` + Since the specific `id` of the `user` is not known beforehand, we can use + `queryRecord` to get the user: + ```javascript + store.queryRecord('user', {}).then(function(user) { + let username = user.get('username'); + console.log(`Currently logged in as ${username}`); + }); + ``` + The request is made through the adapters' `queryRecord`: + ```javascript + // app/adapters/user.js + import Adapter from "ember-data/adapter"; + export default Adapter.extend({ + queryRecord(modelName, query) { + return Ember.$.getJSON("/api/current_user"); + } + }); + ``` + Note: the primary use case for `store.queryRecord` is when a single record + is queried and the `id` is not kown beforehand. In all other cases + `store.query` and using the first item of the array is likely the preferred + way: + ``` + // GET /users?username=unique + { + data: [{ + id: 1234, + type: 'user', + attributes: { + username: "unique" + } + }] + } + ``` + ```javascript + store.query('user', { username: 'unique' }).then(function(users) { + return users.get('firstObject'); + }).then(function(user) { + let id = user.get('id'); + }); + ``` + This method returns a promise, which resolves with the found record. + If the adapter returns no data for the primary data of the payload, then + `queryRecord` resolves with `null`: + ``` + // GET /users?username=unique + { + data: null + } + ``` + ```javascript + store.queryRecord('user', { username: 'unique' }).then(function(user) { + console.log(user); // null + }); + ``` @method queryRecord @param {String} modelName @param {any} query an opaque query to be used by the adapter - @return {Promise} promise + @return {Promise} promise which resolves with the found record or `null` */ queryRecord: function (modelName, query) { var typeClass = this.modelFor(modelName); var adapter = this.adapterFor(modelName); @@ -9950,10 +10013,11 @@ return promise.then(function (adapterPayload) { var record; store._adapterRun(function () { var payload = (0, _emberDataPrivateSystemStoreSerializerResponse.normalizeResponseHelper)(serializer, store, typeClass, adapterPayload, null, 'queryRecord'); + //TODO Optimize record = store.push(payload); }); return record; @@ -13350,10 +13414,16 @@ _normalizeResponse: function (store, primaryModelClass, payload, id, requestType, isSingle) { var normalizedPayload = this._normalizeDocumentHelper(payload); return normalizedPayload; }, + normalizeQueryRecordResponse: function () { + var normalized = this._super.apply(this, arguments); + + return normalized; + }, + /** @method extractAttributes @param {DS.Model} modelClass @param {Object} resourceHash @return {Object} @@ -15791,10 +15861,10 @@ */ deserialize: null }); }); define("ember-data/version", ["exports"], function (exports) { - exports.default = "2.6.0-beta.2"; + exports.default = "2.6.0-beta.3"; }); define("ember-inflector", ["exports", "ember", "ember-inflector/lib/system", "ember-inflector/lib/ext/string"], function (exports, _ember, _emberInflectorLibSystem, _emberInflectorLibExtString) { _emberInflectorLibSystem.Inflector.defaultRules = _emberInflectorLibSystem.defaultRules; _ember.default.Inflector = _emberInflectorLibSystem.Inflector;