From 87b7652745757d1976608698d6dea3a662d2976d Mon Sep 17 00:00:00 2001 From: Ricardo Mendes Date: Sun, 30 Jul 2017 22:32:23 +0100 Subject: [PATCH] Updates imports in documentation to adhere to RFC 176 --- addon/-private/adapters/errors.js | 20 +- addon/-private/system/model/errors.js | 46 ++-- addon/-private/system/model/model.js | 249 +++++++++--------- addon/-private/system/normalize-model-name.js | 4 +- addon/adapter.js | 76 +++--- addon/serializers/json.js | 175 ++++++------ addon/serializers/rest.js | 9 +- addon/transforms/transform.js | 6 +- app/initializers/ember-data.js | 11 +- 9 files changed, 317 insertions(+), 279 deletions(-) diff --git a/addon/-private/adapters/errors.js b/addon/-private/adapters/errors.js index 085df8d7892..65c22b0102d 100644 --- a/addon/-private/adapters/errors.js +++ b/addon/-private/adapters/errors.js @@ -56,10 +56,10 @@ const PRIMARY_ATTRIBUTE_KEY = 'base'; `under-maintenance` route: ```app/routes/application.js - import Ember from 'ember'; + import Route from '@ember/routing/route'; import MaintenanceError from '../adapters/maintenance-error'; - export default Ember.Route.extend({ + export default Route.extend({ actions: { error(error, transition) { if (error instanceof MaintenanceError) { @@ -148,13 +148,13 @@ if (extendedErrorsEnabled) { rejects with a `DS.InvalidError` object that looks like this: ```app/adapters/post.js - import Ember from 'ember'; + import RSVP from 'rsvp'; import DS from 'ember-data'; export default DS.RESTAdapter.extend({ updateRecord() { // Fictional adapter that always rejects - return Ember.RSVP.reject(new DS.InvalidError([ + return RSVP.reject(new DS.InvalidError([ { detail: 'Must be unique', source: { pointer: '/data/attributes/title' } @@ -189,12 +189,12 @@ export const InvalidError = extend(AdapterError, connection if an adapter operation has timed out: ```app/routes/application.js - import Ember from 'ember'; + import Route from '@ember/routing/route'; import DS from 'ember-data'; const { TimeoutError } = DS; - export default Ember.Route.extend({ + export default Route.extend({ actions: { error(error, transition) { if (error instanceof TimeoutError) { @@ -237,12 +237,12 @@ export const AbortError = extend(AdapterError, request is unauthorized: ```app/routes/application.js - import Ember from 'ember'; + import Route from '@ember/routing/route'; import DS from 'ember-data'; const { UnauthorizedError } = DS; - export default Ember.Route.extend({ + export default Route.extend({ actions: { error(error, transition) { if (error instanceof UnauthorizedError) { @@ -285,12 +285,12 @@ export const ForbiddenError = extendedErrorsEnabled ? for a specific model that does not exist. For example: ```app/routes/post.js - import Ember from 'ember'; + import Route from '@ember/routing/route'; import DS from 'ember-data'; const { NotFoundError } = DS; - export default Ember.Route.extend({ + export default Route.extend({ model(params) { return this.get('store').findRecord('post', params.post_id); }, diff --git a/addon/-private/system/model/errors.js b/addon/-private/system/model/errors.js index 42dd3d1c9a3..d9ceb6d3036 100644 --- a/addon/-private/system/model/errors.js +++ b/addon/-private/system/model/errors.js @@ -289,16 +289,17 @@ export default Ember.ArrayProxy.extend(Ember.Evented, { ``` ```app/routes/user/edit.js - import Ember from 'ember'; + import Route from '@ember/routing/route'; - export default Ember.Route.extend({ + export default Route.extend({ actions: { - save: function(user) { - if (!user.get('twoFactorAuth')) { - user.get('errors').remove('phone'); - } - user.save(); - } + save(user) { + if (!user.get('twoFactorAuth')) { + user.get('errors').remove('phone'); + } + + user.save(); + } } }); ``` @@ -344,14 +345,14 @@ export default Ember.ArrayProxy.extend(Ember.Evented, { Example: ```app/routes/user/edit.js - import Ember from 'ember'; + import Route from '@ember/routing/route'; - export default Ember.Route.extend({ + export default Route.extend({ actions: { - retrySave: function(user) { - user.get('errors').clear(); - user.save(); - } + retrySave(user) { + user.get('errors').clear(); + user.save(); + } } }); ``` @@ -401,16 +402,17 @@ export default Ember.ArrayProxy.extend(Ember.Evented, { Checks if there is error messages for the given attribute. ```app/routes/user/edit.js - import Ember from 'ember'; + import Route from '@ember/routing/route'; - export default Ember.Route.extend({ + export default Route.extend({ actions: { - save: function(user) { - if (user.get('errors').has('email')) { - return alert('Please update your email before attempting to save.'); - } - user.save(); - } + save(user) { + if (user.get('errors').has('email')) { + return alert('Please update your email before attempting to save.'); + } + + user.save(); + } } }); ``` diff --git a/addon/-private/system/model/model.js b/addon/-private/system/model/model.js index 354bd46cdd9..a900349f746 100644 --- a/addon/-private/system/model/model.js +++ b/addon/-private/system/model/model.js @@ -544,17 +544,17 @@ const Model = Ember.Object.extend(Ember.Evented, { Example ```app/routes/model/delete.js - import Ember from 'ember'; + import Route from '@ember/routing/route'; - export default Ember.Route.extend({ + export default Route.extend({ actions: { - softDelete: function() { + softDelete() { this.controller.get('model').deleteRecord(); }, - confirm: function() { + confirm() { this.controller.get('model').save(); }, - undo: function() { + undo() { this.controller.get('model').rollbackAttributes(); } } @@ -577,7 +577,7 @@ const Model = Ember.Object.extend(Ember.Evented, { export default Ember.Route.extend({ actions: { - delete: function() { + delete() { let controller = this.controller; controller.get('model').destroyRecord().then(function() { controller.transitionToRoute('model.index'); @@ -598,7 +598,7 @@ const Model = Ember.Object.extend(Ember.Evented, { import MyCustomAdapter from './custom-adapter'; export default MyCustomAdapter.extend({ - deleteRecord: function(store, type, snapshot) { + deleteRecord(store, type, snapshot) { if (snapshot.adapterOptions.subscribe) { // ... } @@ -694,14 +694,14 @@ const Model = Ember.Object.extend(Ember.Evented, { /** @method adapterWillCommit @private - adapterWillCommit: function() { + adapterWillCommit() { this.send('willCommit'); }, /** @method adapterDidDirty @private - adapterDidDirty: function() { + adapterDidDirty() { this.send('becomeDirty'); this.updateRecordArraysLater(); }, @@ -766,7 +766,7 @@ const Model = Ember.Object.extend(Ember.Evented, { import MyCustomAdapter from './custom-adapter'; export default MyCustomAdapter.extend({ - updateRecord: function(store, type, snapshot) { + updateRecord(store, type, snapshot) { if (snapshot.adapterOptions.subscribe) { // ... } @@ -798,7 +798,7 @@ const Model = Ember.Object.extend(Ember.Evented, { export default Ember.Route.extend({ actions: { - reload: function() { + reload() { this.controller.get('model').reload().then(function(model) { // do something with the reloaded model }); @@ -1075,7 +1075,7 @@ const Model = Ember.Object.extend(Ember.Evented, { import DS from 'ember-data'; export default DS.JSONSerializer.extend({ - serialize: function(record, options) { + serialize(record, options) { let json = {}; record.eachRelationship(function(name, descriptor) { @@ -1172,10 +1172,13 @@ Model.reopenClass({ The most common place you'll want to access `modelName` is in your serializer's `payloadKeyFromModelName` method. For example, to change payload keys to underscore (instead of dasherized), you might use the following code: - ```javascript + ```app/serializers/post.js + import DS from 'ember-data'; + import { underscore } from '@ember/string'; + export default const PostSerializer = DS.RESTSerializer.extend({ - payloadKeyFromModelName: function(modelName) { - return Ember.String.underscore(modelName); + payloadKeyFromModelName(modelName) { + return underscore(modelName); } }); ``` @@ -1683,43 +1686,43 @@ Model.reopenClass({ }).readOnly(), /** - A map whose keys are the attributes of the model (properties - described by DS.attr) and whose values are type of transformation - applied to each attribute. This map does not include any - attributes that do not have an transformation type. + A map whose keys are the attributes of the model (properties + described by DS.attr) and whose values are type of transformation + applied to each attribute. This map does not include any + attributes that do not have an transformation type. - Example + Example - ```app/models/person.js - import DS from 'ember-data'; + ```app/models/person.js + import DS from 'ember-data'; - export default DS.Model.extend({ - firstName: DS.attr(), - lastName: DS.attr('string'), - birthday: DS.attr('date') - }); - ``` + export default DS.Model.extend({ + firstName: DS.attr(), + lastName: DS.attr('string'), + birthday: DS.attr('date') + }); + ``` - ```javascript - import Ember from 'ember'; - import Person from 'app/models/person'; + ```javascript + import { get } from '@ember/object'; + import Person from 'app/models/person'; - let transformedAttributes = Ember.get(Person, 'transformedAttributes') + let transformedAttributes = get(Person, 'transformedAttributes') - transformedAttributes.forEach(function(field, type) { - console.log(field, type); - }); + transformedAttributes.forEach(function(field, type) { + console.log(field, type); + }); - // prints: - // lastName string - // birthday date - ``` + // prints: + // lastName string + // birthday date + ``` - @property transformedAttributes - @static - @type {Ember.Map} - @readOnly - */ + @property transformedAttributes + @static + @type {Ember.Map} + @readOnly + */ transformedAttributes: Ember.computed(function() { let map = Map.create(); @@ -1733,48 +1736,48 @@ Model.reopenClass({ }).readOnly(), /** - Iterates through the attributes of the model, calling the passed function on each - attribute. + Iterates through the attributes of the model, calling the passed function on each + attribute. - The callback method you provide should have the following signature (all - parameters are optional): + The callback method you provide should have the following signature (all + parameters are optional): - ```javascript - function(name, meta); - ``` + ```javascript + function(name, meta); + ``` - - `name` the name of the current property in the iteration - - `meta` the meta object for the attribute property in the iteration + - `name` the name of the current property in the iteration + - `meta` the meta object for the attribute property in the iteration - Note that in addition to a callback, you can also pass an optional target - object that will be set as `this` on the context. + Note that in addition to a callback, you can also pass an optional target + object that will be set as `this` on the context. - Example + Example - ```javascript - import DS from 'ember-data'; + ```javascript + import DS from 'ember-data'; - let Person = DS.Model.extend({ + let Person = DS.Model.extend({ firstName: DS.attr('string'), lastName: DS.attr('string'), birthday: DS.attr('date') }); - Person.eachAttribute(function(name, meta) { + Person.eachAttribute(function(name, meta) { console.log(name, meta); }); - // prints: - // firstName {type: "string", isAttribute: true, options: Object, parentType: function, name: "firstName"} - // lastName {type: "string", isAttribute: true, options: Object, parentType: function, name: "lastName"} - // birthday {type: "date", isAttribute: true, options: Object, parentType: function, name: "birthday"} - ``` + // prints: + // firstName {type: "string", isAttribute: true, options: Object, parentType: function, name: "firstName"} + // lastName {type: "string", isAttribute: true, options: Object, parentType: function, name: "lastName"} + // birthday {type: "date", isAttribute: true, options: Object, parentType: function, name: "birthday"} + ``` - @method eachAttribute - @param {Function} callback The callback to execute - @param {Object} [binding] the value to which the callback's `this` should be bound - @static - */ + @method eachAttribute + @param {Function} callback The callback to execute + @param {Object} [binding] the value to which the callback's `this` should be bound + @static + */ eachAttribute(callback, binding) { get(this, 'attributes').forEach((meta, name) => { callback.call(binding, name, meta); @@ -1782,49 +1785,49 @@ Model.reopenClass({ }, /** - Iterates through the transformedAttributes of the model, calling - the passed function on each attribute. Note the callback will not be - called for any attributes that do not have an transformation type. + Iterates through the transformedAttributes of the model, calling + the passed function on each attribute. Note the callback will not be + called for any attributes that do not have an transformation type. - The callback method you provide should have the following signature (all - parameters are optional): + The callback method you provide should have the following signature (all + parameters are optional): - ```javascript - function(name, type); - ``` + ```javascript + function(name, type); + ``` - - `name` the name of the current property in the iteration - - `type` a string containing the name of the type of transformed - applied to the attribute + - `name` the name of the current property in the iteration + - `type` a string containing the name of the type of transformed + applied to the attribute - Note that in addition to a callback, you can also pass an optional target - object that will be set as `this` on the context. + Note that in addition to a callback, you can also pass an optional target + object that will be set as `this` on the context. - Example + Example - ```javascript - import DS from 'ember-data'; + ```javascript + import DS from 'ember-data'; - let Person = DS.Model.extend({ - firstName: DS.attr(), - lastName: DS.attr('string'), - birthday: DS.attr('date') - }); + let Person = DS.Model.extend({ + firstName: DS.attr(), + lastName: DS.attr('string'), + birthday: DS.attr('date') + }); - Person.eachTransformedAttribute(function(name, type) { - console.log(name, type); - }); + Person.eachTransformedAttribute(function(name, type) { + console.log(name, type); + }); - // prints: - // lastName string - // birthday date - ``` + // prints: + // lastName string + // birthday date + ``` - @method eachTransformedAttribute - @param {Function} callback The callback to execute - @param {Object} [binding] the value to which the callback's `this` should be bound - @static - */ + @method eachTransformedAttribute + @param {Function} callback The callback to execute + @param {Object} [binding] the value to which the callback's `this` should be bound + @static + */ eachTransformedAttribute(callback, binding) { get(this, 'transformedAttributes').forEach((type, name) => { callback.call(binding, name, type); @@ -1887,31 +1890,31 @@ if (DEBUG) { }, /** - This Ember.js hook allows an object to be notified when a property - is defined. + This Ember.js hook allows an object to be notified when a property + is defined. - In this case, we use it to be notified when an Ember Data user defines a - belongs-to relationship. In that case, we need to set up observers for - each one, allowing us to track relationship changes and automatically - reflect changes in the inverse has-many array. + In this case, we use it to be notified when an Ember Data user defines a + belongs-to relationship. In that case, we need to set up observers for + each one, allowing us to track relationship changes and automatically + reflect changes in the inverse has-many array. - This hook passes the class being set up, as well as the key and value - being defined. So, for example, when the user does this: + This hook passes the class being set up, as well as the key and value + being defined. So, for example, when the user does this: - ```javascript - DS.Model.extend({ - parent: DS.belongsTo('user') - }); - ``` + ```javascript + DS.Model.extend({ + parent: DS.belongsTo('user') + }); + ``` - This hook would be called with "parent" as the key and the computed - property returned by `DS.belongsTo` as the value. + This hook would be called with "parent" as the key and the computed + property returned by `DS.belongsTo` as the value. - @method didDefineProperty - @param {Object} proto - @param {String} key - @param {Ember.ComputedProperty} value - */ + @method didDefineProperty + @param {Object} proto + @param {String} key + @param {Ember.ComputedProperty} value + */ didDefineProperty(proto, key, value) { // Check if the value being set is a computed property. if (value instanceof Ember.ComputedProperty) { diff --git a/addon/-private/system/normalize-model-name.js b/addon/-private/system/normalize-model-name.js index f885e5ddd61..07885e8a552 100644 --- a/addon/-private/system/normalize-model-name.js +++ b/addon/-private/system/normalize-model-name.js @@ -4,8 +4,8 @@ import Ember from 'ember'; // require changes to other normalization hooks (such as typeForRoot). /** - This method normalizes a modelName into the format Ember Data uses - internally. + This method normalizes a modelName into the format Ember Data uses + internally. @method normalizeModelName @public diff --git a/addon/adapter.js b/addon/adapter.js index 64b4e8c6be6..6ebb94e6405 100644 --- a/addon/adapter.js +++ b/addon/adapter.js @@ -94,13 +94,14 @@ export default Ember.Object.extend({ Here is an example `findRecord` implementation: ```app/adapters/application.js - import Ember from 'ember'; + import { Promise } from 'rsvp'; + import $ from 'jquery'; import DS from 'ember-data'; export default DS.Adapter.extend({ findRecord(store, type, id, snapshot) { - return new Ember.RSVP.Promise(function(resolve, reject) { - Ember.$.getJSON(`/${type.modelName}/${id}`).then(function(data) { + return new Promise(function(resolve, reject) { + $.getJSON(`/${type.modelName}/${id}`).then(function(data) { resolve(data); }, function(jqXHR) { reject(jqXHR); @@ -125,15 +126,16 @@ export default Ember.Object.extend({ Example ```app/adapters/application.js - import Ember from 'ember'; + import { Promise } from 'rsvp'; + import $ from 'jquery'; import DS from 'ember-data'; export default DS.Adapter.extend({ findAll(store, type, sinceToken) { let query = { since: sinceToken }; - return new Ember.RSVP.Promise(function(resolve, reject) { - Ember.$.getJSON(`/${type.modelName}`, query).then(function(data) { + return new Promise(function(resolve, reject) { + $.getJSON(`/${type.modelName}`, query).then(function(data) { resolve(data); }, function(jqXHR) { reject(jqXHR); @@ -158,13 +160,14 @@ export default Ember.Object.extend({ Example ```app/adapters/application.js - import Ember from 'ember'; + import { Promise } from 'rsvp'; + import $ from 'jquery'; import DS from 'ember-data'; export default DS.Adapter.extend({ query(store, type, query) { - return new Ember.RSVP.Promise(function(resolve, reject) { - Ember.$.getJSON(`/${type.modelName}`, query).then(function(data) { + return new Promise(function(resolve, reject) { + $.getJSON(`/${type.modelName}`, query).then(function(data) { resolve(data); }, function(jqXHR) { reject(jqXHR); @@ -196,13 +199,14 @@ export default Ember.Object.extend({ Example ```app/adapters/application.js - import Ember from 'ember'; + import { Promise } from 'rsvp'; + import $ from 'jquery'; import DS from 'ember-data'; export default DS.Adapter.extend(DS.BuildURLMixin, { queryRecord(store, type, query) { - return new Ember.RSVP.Promise(function(resolve, reject) { - Ember.$.getJSON(`/${type.modelName}`, query).then(function(data) { + return new Promise(function(resolve, reject) { + $.getJSON(`/${type.modelName}`, query).then(function(data) { resolve(data); }, function(jqXHR) { reject(jqXHR); @@ -290,24 +294,26 @@ export default Ember.Object.extend({ Example ```app/adapters/application.js - import Ember from 'ember'; + import { Promise } from 'rsvp'; + import $ from 'jquery'; + import { run } from '@ember/runloop'; import DS from 'ember-data'; export default DS.Adapter.extend({ createRecord(store, type, snapshot) { let data = this.serialize(snapshot, { includeId: true }); - return new Ember.RSVP.Promise(function(resolve, reject) { - Ember.$.ajax({ + return new Promise(function(resolve, reject) { + $.ajax({ type: 'POST', url: `/${type.modelName}`, dataType: 'json', data: data }).then(function(data) { - Ember.run(null, resolve, data); + run(null, resolve, data); }, function(jqXHR) { jqXHR.then = null; // tame jQuery's ill mannered promises - Ember.run(null, reject, jqXHR); + run(null, reject, jqXHR); }); }); } @@ -339,7 +345,9 @@ export default Ember.Object.extend({ Example ```app/adapters/application.js - import Ember from 'ember'; + import { Promise } from 'rsvp'; + import $ from 'jquery'; + import { run } from '@ember/runloop'; import DS from 'ember-data'; export default DS.Adapter.extend({ @@ -347,17 +355,17 @@ export default Ember.Object.extend({ let data = this.serialize(snapshot, { includeId: true }); let id = snapshot.id; - return new Ember.RSVP.Promise(function(resolve, reject) { - Ember.$.ajax({ + return new Promise(function(resolve, reject) { + $.ajax({ type: 'PUT', url: `/${type.modelName}/${id}`, dataType: 'json', data: data }).then(function(data) { - Ember.run(null, resolve, data); + run(null, resolve, data); }, function(jqXHR) { jqXHR.then = null; // tame jQuery's ill mannered promises - Ember.run(null, reject, jqXHR); + run(null, reject, jqXHR); }); }); } @@ -381,7 +389,9 @@ export default Ember.Object.extend({ Example ```app/adapters/application.js - import Ember from 'ember'; + import { Promise } from 'rsvp'; + import $ from 'jquery'; + import { run } from '@ember/runloop'; import DS from 'ember-data'; export default DS.Adapter.extend({ @@ -389,17 +399,17 @@ export default Ember.Object.extend({ let data = this.serialize(snapshot, { includeId: true }); let id = snapshot.id; - return new Ember.RSVP.Promise(function(resolve, reject) { - Ember.$.ajax({ + return new Promise(function(resolve, reject) { + $.ajax({ type: 'DELETE', url: `/${type.modelName}/${id}`, dataType: 'json', data: data }).then(function(data) { - Ember.run(null, resolve, data); + run(null, resolve, data); }, function(jqXHR) { jqXHR.then = null; // tame jQuery's ill mannered promises - Ember.run(null, reject, jqXHR); + run(null, reject, jqXHR); }); }); } @@ -431,22 +441,24 @@ export default Ember.Object.extend({ is true. ```app/adapters/application.js - import Ember from 'ember'; + import { Promise } from 'rsvp'; + import $ from 'jquery'; + import { run } from '@ember/runloop'; import DS from 'ember-data'; export default DS.Adapter.extend({ findMany(store, type, ids, snapshots) { - return new Ember.RSVP.Promise(function(resolve, reject) { - Ember.$.ajax({ + return new Promise(function(resolve, reject) { + $.ajax({ type: 'GET', url: `/${type.modelName}/`, dataType: 'json', data: { filter: { id: ids.join(',') } } }).then(function(data) { - Ember.run(null, resolve, data); + run(null, resolve, data); }, function(jqXHR) { jqXHR.then = null; // tame jQuery's ill mannered promises - Ember.run(null, reject, jqXHR); + run(null, reject, jqXHR); }); }); } diff --git a/addon/serializers/json.js b/addon/serializers/json.js index f624bd43f1f..4808cf906a3 100644 --- a/addon/serializers/json.js +++ b/addon/serializers/json.js @@ -502,13 +502,15 @@ const JSONSerializer = Serializer.extend({ ```app/serializers/application.js import DS from 'ember-data'; + import { get } from '@ember/object'; + import { underscore } from '@ember/string'; export default DS.JSONSerializer.extend({ normalize(typeClass, hash) { - var fields = Ember.get(typeClass, 'fields'); + var fields = get(typeClass, 'fields'); fields.forEach(function(field) { - var payloadField = Ember.String.underscore(field); + var payloadField = underscore(field); if (field === payloadField) { return; } hash[field] = hash[payloadField]; @@ -1068,10 +1070,11 @@ const JSONSerializer = Serializer.extend({ ```app/serializers/application.js import DS from 'ember-data'; + import { decamelize } from '@ember/string'; export default DS.RESTSerializer.extend({ serializeIntoHash(data, type, snapshot, options) { - var root = Ember.String.decamelize(type.modelName); + var root = decamelize(type.modelName); data[root] = this.serialize(snapshot, options); } }); @@ -1135,30 +1138,31 @@ const JSONSerializer = Serializer.extend({ }, /** - `serializeBelongsTo` can be used to customize how `DS.belongsTo` - properties are serialized. + `serializeBelongsTo` can be used to customize how `DS.belongsTo` + properties are serialized. - Example + Example - ```app/serializers/post.js - import DS from 'ember-data'; + ```app/serializers/post.js + import DS from 'ember-data'; + import { isNone } from '@ember/utils'; - export default DS.JSONSerializer.extend({ - serializeBelongsTo(snapshot, json, relationship) { - var key = relationship.key; - var belongsTo = snapshot.belongsTo(key); + export default DS.JSONSerializer.extend({ + serializeBelongsTo(snapshot, json, relationship) { + var key = relationship.key; + var belongsTo = snapshot.belongsTo(key); - key = this.keyForRelationship ? this.keyForRelationship(key, "belongsTo", "serialize") : key; + key = this.keyForRelationship ? this.keyForRelationship(key, "belongsTo", "serialize") : key; - json[key] = Ember.isNone(belongsTo) ? belongsTo : belongsTo.record.toJSON(); - } - }); - ``` + json[key] = isNone(belongsTo) ? belongsTo : belongsTo.record.toJSON(); + } + }); + ``` - @method serializeBelongsTo - @param {DS.Snapshot} snapshot - @param {Object} json - @param {Object} relationship + @method serializeBelongsTo + @param {DS.Snapshot} snapshot + @param {Object} json + @param {Object} relationship */ serializeBelongsTo(snapshot, json, relationship) { let key = relationship.key; @@ -1245,6 +1249,7 @@ const JSONSerializer = Serializer.extend({ ```app/serializers/comment.js import DS from 'ember-data'; + import { isNone } from '@ember/utils'; export default DS.JSONSerializer.extend({ serializePolymorphicType(snapshot, json, relationship) { @@ -1253,7 +1258,7 @@ const JSONSerializer = Serializer.extend({ key = this.keyForAttribute ? this.keyForAttribute(key, 'serialize') : key; - if (Ember.isNone(belongsTo)) { + if (isNone(belongsTo)) { json[key + '_type'] = null; } else { json[key + '_type'] = belongsTo.modelName; @@ -1415,65 +1420,67 @@ const JSONSerializer = Serializer.extend({ }, /** - `keyForAttribute` can be used to define rules for how to convert an - attribute name in your model to a key in your JSON. + `keyForAttribute` can be used to define rules for how to convert an + attribute name in your model to a key in your JSON. - Example + Example - ```app/serializers/application.js - import DS from 'ember-data'; + ```app/serializers/application.js + import DS from 'ember-data'; + import { underscore } from '@ember/string'; - export default DS.RESTSerializer.extend({ - keyForAttribute(attr, method) { - return Ember.String.underscore(attr).toUpperCase(); - } - }); - ``` + export default DS.RESTSerializer.extend({ + keyForAttribute(attr, method) { + return underscore(attr).toUpperCase(); + } + }); + ``` - @method keyForAttribute - @param {String} key - @param {String} method - @return {String} normalized key + @method keyForAttribute + @param {String} key + @param {String} method + @return {String} normalized key */ keyForAttribute(key, method) { return key; }, /** - `keyForRelationship` can be used to define a custom key when - serializing and deserializing relationship properties. By default - `JSONSerializer` does not provide an implementation of this method. + `keyForRelationship` can be used to define a custom key when + serializing and deserializing relationship properties. By default + `JSONSerializer` does not provide an implementation of this method. - Example + Example - ```app/serializers/post.js - import DS from 'ember-data'; + ```app/serializers/post.js + import DS from 'ember-data'; + import { underscore } from '@ember/string'; - export default DS.JSONSerializer.extend({ - keyForRelationship(key, relationship, method) { - return 'rel_' + Ember.String.underscore(key); - } - }); - ``` + export default DS.JSONSerializer.extend({ + keyForRelationship(key, relationship, method) { + return 'rel_' + underscore(key); + } + }); + ``` - @method keyForRelationship - @param {String} key - @param {String} typeClass - @param {String} method - @return {String} normalized key + @method keyForRelationship + @param {String} key + @param {String} typeClass + @param {String} method + @return {String} normalized key */ keyForRelationship(key, typeClass, method) { return key; }, /** - `keyForLink` can be used to define a custom key when deserializing link - properties. + `keyForLink` can be used to define a custom key when deserializing link + properties. - @method keyForLink - @param {String} key - @param {String} kind `belongsTo` or `hasMany` - @return {String} normalized key + @method keyForLink + @param {String} key + @param {String} kind `belongsTo` or `hasMany` + @return {String} normalized key */ keyForLink(key, kind) { return key; @@ -1482,11 +1489,11 @@ const JSONSerializer = Serializer.extend({ // HELPERS /** - @method transformFor - @private - @param {String} attributeType - @param {Boolean} skipAssertion - @return {DS.Transform} transform + @method transformFor + @private + @param {String} attributeType + @param {Boolean} skipAssertion + @return {DS.Transform} transform */ transformFor(attributeType, skipAssertion) { let transform = getOwner(this).lookup('transform:' + attributeType); @@ -1524,28 +1531,28 @@ if (isEnabled("ds-serialize-id")) { JSONSerializer.reopen({ /** - serializeId can be used to customize how id is serialized - For example, your server may expect integer datatype of id + `serializeId` can be used to customize how id is serialized + For example, your server may expect integer datatype of id - By default the snapshot's id (String) is set on the json hash via json[primaryKey] = snapshot.id. + By default the snapshot's id (String) is set on the json hash via `json[primaryKey] = snapshot.id`. - ```app/serializers/application.js - import DS from 'ember-data'; + ```app/serializers/application.js + import DS from 'ember-data'; - export default DS.JSONSerializer.extend({ - serializeId(snapshot, json, primaryKey) { - var id = snapshot.id; - json[primaryKey] = parseInt(id, 10); - } - }); - ``` - - @method serializeId - @public - @param {DS.Snapshot} snapshot - @param {Object} json - @param {String} primaryKey - */ + export default DS.JSONSerializer.extend({ + serializeId(snapshot, json, primaryKey) { + var id = snapshot.id; + json[primaryKey] = parseInt(id, 10); + } + }); + ``` + + @method serializeId + @public + @param {DS.Snapshot} snapshot + @param {Object} json + @param {String} primaryKey + */ serializeId(snapshot, json, primaryKey) { let id = snapshot.id; diff --git a/addon/serializers/rest.js b/addon/serializers/rest.js index 761ff6405c7..88d1678e5a9 100644 --- a/addon/serializers/rest.js +++ b/addon/serializers/rest.js @@ -37,10 +37,11 @@ const { camelize } = Ember.String; ```app/serializers/application.js import DS from 'ember-data'; + import { underscore } from '@ember/string'; export default DS.RESTSerializer.extend({ keyForAttribute(attr, method) { - return Ember.String.underscore(attr).toUpperCase(); + return underscore(attr).toUpperCase(); } }); ``` @@ -668,10 +669,11 @@ const RESTSerializer = JSONSerializer.extend({ ```app/serializers/application.js import DS from 'ember-data'; + import { decamelize } from '@ember/string'; export default DS.RESTSerializer.extend({ serializeIntoHash(data, type, record, options) { - var root = Ember.String.decamelize(type.modelName); + var root = decamelize(type.modelName); data[root] = this.serialize(record, options); } }); @@ -709,10 +711,11 @@ const RESTSerializer = JSONSerializer.extend({ ```app/serializers/application.js import DS from 'ember-data'; + import { dasherize } from '@ember/string'; export default DS.RESTSerializer.extend({ payloadKeyFromModelName(modelName) { - return Ember.String.dasherize(modelName); + return dasherize(modelName); } }); ``` diff --git a/addon/transforms/transform.js b/addon/transforms/transform.js index a13cc3fe527..524007d8519 100644 --- a/addon/transforms/transform.js +++ b/addon/transforms/transform.js @@ -75,8 +75,12 @@ export default Ember.Object.extend({ Example ```javascript + import { isEmpty } from '@ember/utils'; + + … + serialize(deserialized, options) { - return Ember.isEmpty(deserialized) ? null : Number(deserialized); + return isEmpty(deserialized) ? null : Number(deserialized); } ``` diff --git a/app/initializers/ember-data.js b/app/initializers/ember-data.js index ba03dbbc552..b610bfd0ae9 100644 --- a/app/initializers/ember-data.js +++ b/app/initializers/ember-data.js @@ -15,11 +15,18 @@ import 'ember-data'; For example, imagine an Ember.js application with the following classes: - App.StoreService = DS.Store.extend({ + ```app/services/store.js + import DS from 'ember-data'; + + export default DS.Store.extend({ adapter: 'custom' }); + ``` + + ```app/controllers/posts.js + import Controller from '@ember/routing/route'; - App.PostsController = Ember.Controller.extend({ + export default Controller.extend({ // ... });