ember-data-partial-model
Advanced tools
Comparing version
import Ember from 'ember'; | ||
const { Mixin, A: emberA } = Ember; | ||
const { Mixin } = Ember; | ||
@@ -8,39 +8,10 @@ export default Mixin.create({ | ||
if (typeClass._isPartialModel === true) { | ||
if (typeClass._isPartialModel) { | ||
let partialDescriptors = this._partialDescriptors(typeClass); | ||
let attributesForExtensions = this._extractExtensionsAttributes(partialDescriptors, hash); | ||
normalizedHash = this._super(typeClass, hash, prop); | ||
partialDescriptors.forEach(descriptor => { | ||
let extensionKey = descriptor.key; | ||
let extensionName = descriptor.type; | ||
let attributesForExtension = attributesForExtensions[extensionName]; | ||
normalizedHash = this._assignExtensionsRelationships(normalizedHash, extensionKey); | ||
this._updateModelAttributes(extensionName, attributesForExtension); | ||
hash[descriptor.key] = hash.id; | ||
}); | ||
} else if (typeClass._extendPartialModel) { | ||
let parentFactoryName = typeClass._extendPartialModel; | ||
let parentTypeClass = this.container.lookupFactory(`model:${parentFactoryName}`); | ||
let partialDescriptors = this._partialDescriptors(parentTypeClass); | ||
let attributesForExtensions = this._extractExtensionsAttributes(partialDescriptors, hash); | ||
let currentExtensionName = typeClass.modelName; | ||
// Normalize for the current extension | ||
let attributesForCurrentExtension = attributesForExtensions[currentExtensionName]; | ||
normalizedHash = this._super(typeClass, attributesForCurrentExtension, prop); | ||
// Update parent attributes | ||
this._updateModelAttributes(parentFactoryName, hash); | ||
// Update other partial extensions attributes | ||
partialDescriptors.forEach(descriptor => { | ||
let extensionName = descriptor.type; | ||
let attributesForExtension = attributesForExtensions[extensionName]; | ||
if (extensionName !== currentExtensionName) { | ||
this._updateModelAttributes(extensionName, attributesForExtension); | ||
} | ||
}); | ||
normalizedHash = this._super(typeClass, hash, prop); | ||
} else { | ||
@@ -73,41 +44,11 @@ normalizedHash = this._super(...arguments); | ||
_partialDescriptors: function(typeClass) { | ||
let descriptors = emberA(); | ||
typeClass.eachRelationship((relationshipKey, descriptor) => { | ||
if (descriptor.options.isPartialExtension) { | ||
descriptors.push(descriptor); | ||
} | ||
}); | ||
return descriptors; | ||
}, | ||
_extractExtensionsAttributes: function(partialDescriptors, hash) { | ||
let attributesForExtensions = {}; | ||
partialDescriptors.forEach(descriptor => { | ||
let extensionName = descriptor.type; | ||
attributesForExtensions[extensionName] = {}; | ||
Object.keys(descriptor.options.classHash).forEach(attr => { | ||
if (hash[attr]) { | ||
attributesForExtensions[extensionName][attr] = hash[attr]; | ||
delete hash[attr]; | ||
} | ||
}); | ||
attributesForExtensions[extensionName]['id'] = hash['id']; | ||
}); | ||
return attributesForExtensions; | ||
}, | ||
_assignExtensionsRelationships: function(normalizedHash, extensionKey) { | ||
normalizedHash[extensionKey] = normalizedHash.id; | ||
return normalizedHash; | ||
}, | ||
_updateModelAttributes: function(modelName, attributesForModel) { | ||
if (Object.keys(attributesForModel).length > 1) { | ||
this.store.push(modelName, attributesForModel); | ||
// RETHINK: add _partialDescriptors to snapshots? | ||
if (typeClass._partialDescriptors) { | ||
return typeClass._partialDescriptors(); | ||
} else if(typeClass.type && typeClass.type._partialDescriptors) { | ||
return typeClass.type._partialDescriptors(); | ||
} else { | ||
return []; | ||
} | ||
} | ||
}); |
@@ -17,2 +17,3 @@ import Ember from 'ember'; | ||
createRecord: function(modelName, inputProperties) { | ||
inputProperties = inputProperties || {}; | ||
let factory = this.modelFor(modelName); | ||
@@ -25,7 +26,8 @@ let relationshipsToBeAssigned = {}; | ||
if (descriptor.options.isPartialExtension === true) { | ||
// TODO: check for properties in parent model and do not set them in partial models | ||
let partialModelName = `${modelName}-${relationshipKey}`; | ||
let partialModel = this._super(partialModelName, inputProperties); | ||
let attributesFromPartialModel = Object.keys(descriptor.options.classHash); | ||
let propertiesForPartialModel = this._extractPropertiesForPartialModel(inputProperties, attributesFromPartialModel); | ||
let partialModel = this._super(partialModelName, propertiesForPartialModel); | ||
attributesFromPartialModels = attributesFromPartialModels.concat(Object.keys(descriptor.options.classHash)); | ||
attributesFromPartialModels = attributesFromPartialModels.concat(attributesFromPartialModel); | ||
relationshipsToBeAssigned[relationshipKey] = partialModel; | ||
@@ -55,10 +57,19 @@ } | ||
_extractPropertiesForPartialModel: function(inputProperties, attributesFromPartialModel) { | ||
let propertiesForPartialModel = {}; | ||
attributesFromPartialModel.forEach((prop) => { | ||
propertiesForPartialModel[prop] = inputProperties[prop]; | ||
}); | ||
return propertiesForPartialModel; | ||
}, | ||
_generatePartialExtensionModel: function(factoryName, factory) { | ||
let registry = _getRegistry(this); | ||
factory.eachRelationship((relationshipKey, descriptor) => { | ||
let partialExtensionModelName = `${factoryName}-${relationshipKey}`; | ||
if (descriptor.options.isPartialExtension === true) { | ||
if (!this.container.has(`model:${partialExtensionModelName}`)) { | ||
if (!registry.has(`model:${partialExtensionModelName}`)) { | ||
let partialExtensionModel = Model.extend(descriptor.options.classHash) | ||
.reopenClass({ _extendPartialModel: factoryName }); | ||
this.container.register(`model:${partialExtensionModelName}`, partialExtensionModel); | ||
registry.register(`model:${partialExtensionModelName}`, partialExtensionModel); | ||
} | ||
@@ -70,6 +81,7 @@ } | ||
_generatePartialExtensionSerializer: function(factoryName, factory) { | ||
let registry = _getRegistry(this); | ||
factory.eachRelationship((relationshipKey, descriptor) => { | ||
let partialExtensionSerializerName = `${factoryName}-${relationshipKey}`; | ||
if (descriptor.options.isPartialExtension === true) { | ||
if (!this.container.has(`serializer:${partialExtensionSerializerName}`)) { | ||
if (!registry.has(`serializer:${partialExtensionSerializerName}`)) { | ||
let parentSerializerClass = this.serializerFor(factoryName).constructor; | ||
@@ -81,3 +93,3 @@ let partialExtensionSerializer = parentSerializerClass.extend({ | ||
}); | ||
this.container.register(`serializer:${partialExtensionSerializerName}`, | ||
registry.register(`serializer:${partialExtensionSerializerName}`, | ||
partialExtensionSerializer); | ||
@@ -89,1 +101,10 @@ } | ||
}); | ||
function _getRegistry(store) { | ||
// support pre Ember 2.1.x (Ember 2.0.x, 1.13.x) | ||
if (store.container._registry) { | ||
return store.container._registry; | ||
} else { | ||
return store.container.registry; | ||
} | ||
} |
@@ -5,3 +5,3 @@ import DS from 'ember-data'; | ||
const { alias } = computed; | ||
const { Model, belongsTo } = DS; | ||
const { Model, belongsTo, PromiseObject } = DS; | ||
@@ -16,9 +16,3 @@ function partial(modelName, prop, hash) { | ||
_partialDescriptors() { | ||
let descriptors = emberA(); | ||
this.constructor.eachRelationship((relationshipKey, descriptor) => { | ||
if (descriptor.options.isPartialExtension) { | ||
descriptors.push(descriptor); | ||
} | ||
}); | ||
return descriptors; | ||
return this.constructor._partialDescriptors(); | ||
}, | ||
@@ -38,2 +32,18 @@ | ||
}); | ||
}, | ||
save() { | ||
var model = this; | ||
if (model.constructor._isPartialModel) { | ||
return model.loadPartials().then(() => { | ||
// Why this._super() doesn't work here? | ||
return PromiseObject.create({ | ||
promise: model._internalModel.save().then(function() { | ||
return model; | ||
}) | ||
}); | ||
}); | ||
} else { | ||
return this._super(...arguments); | ||
} | ||
} | ||
@@ -43,2 +53,11 @@ }); | ||
PartialModel.reopenClass({ | ||
_partialDescriptors: function() { | ||
let descriptors = emberA(); | ||
this.eachRelationship((relationshipKey, descriptor) => { | ||
if (descriptor.options.isPartialExtension) { | ||
descriptors.push(descriptor); | ||
} | ||
}); | ||
return descriptors; | ||
}, | ||
_isPartialModel: true | ||
@@ -45,0 +64,0 @@ }); |
@@ -5,2 +5,2 @@ import DS from 'ember-data'; | ||
export default RESTAdapter.extend(PartialModelAdapter, {}); | ||
export default RESTAdapter.extend(PartialModelAdapter); |
@@ -5,2 +5,2 @@ import DS from 'ember-data'; | ||
export default RESTSerializer.extend(PartialModelSerializer, {}); | ||
export default RESTSerializer.extend(PartialModelSerializer); |
module.exports = { | ||
scenarios: [ | ||
{ | ||
name: '2.0', | ||
dependencies: { | ||
'ember': '2.0.0', | ||
'ember-data': '2.0.0' | ||
} | ||
}, | ||
{ | ||
name: 'default', | ||
dependencies: { } | ||
dependencies: { | ||
} | ||
}, | ||
@@ -7,0 +16,0 @@ { |
{ | ||
"name": "ember-data-partial-model", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Add support for partial models to Ember Data.", | ||
@@ -22,13 +22,15 @@ "directories": { | ||
"broccoli-asset-rev": "^2.0.2", | ||
"ember-cli": "0.2.6", | ||
"ember-cli-app-version": "0.3.3", | ||
"ember-cli": "1.13.8", | ||
"ember-cli-app-version": "1.0.0", | ||
"ember-cli-content-security-policy": "0.4.0", | ||
"ember-cli-dependency-checker": "^1.0.0", | ||
"ember-cli-htmlbars": "0.7.6", | ||
"ember-cli-es5-shim": "^0.1.1", | ||
"ember-cli-htmlbars": "1.0.1", | ||
"ember-cli-ic-ajax": "0.1.1", | ||
"ember-cli-inject-live-reload": "^1.3.0", | ||
"ember-cli-mirage": "^0.1.4", | ||
"ember-cli-mirage": "^0.1.11", | ||
"ember-cli-pretender": "0.5.0", | ||
"ember-cli-qunit": "0.3.13", | ||
"ember-cli-uglify": "^1.0.1", | ||
"ember-data": "1.0.0-beta.18", | ||
"ember-data": "2.1.0", | ||
"ember-disable-prototype-extensions": "^1.0.0", | ||
@@ -35,0 +37,0 @@ "ember-disable-proxy-controllers": "^1.0.0", |
16
6.67%280
4.09%14089
-3.37%18
12.5%