-
-
Save toranb/9750493 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
function buildRecord(type, data, store) { | |
var containerKey = 'model:' + type; | |
var factory = store.container.lookupFactory(containerKey); | |
var record = factory.create({ | |
id: data.id, | |
$data: data | |
}); | |
var id = data.id; | |
identityMapForType(type, store)[id] = record; | |
return record; | |
} | |
function identityMapForType(type, store) { | |
var typeIdentityMap = store.get('identityMap'); | |
var idIdentityMap = typeIdentityMap[type] || {}; | |
typeIdentityMap[type] = idIdentityMap; | |
return idIdentityMap; | |
} | |
export default Ember.Object.extend({ | |
init: function() { | |
this.set('identityMap', {}); | |
}, | |
push: function(type, data) { | |
var record = this.getById(type, data.id); | |
if (record) { | |
record.set('$data', data); | |
} else { | |
record = buildRecord(type, data, this); | |
} | |
return record; | |
}, | |
getById: function(type, id) { | |
var identityMap = identityMapForType(type, this); | |
return identityMap[id] || null; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment