Created
November 24, 2016 15:58
-
-
Save diamondo25/6101ae532fed2f19bd822a7ce9975f0e to your computer and use it in GitHub Desktop.
Use integer / number id in Ember without canary build
This file contains 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
// app/serializers/application.js | |
import DS from 'ember-data'; | |
import Ember from 'ember'; | |
import coerceId from "ember-data/-private/system/coerce-id"; | |
var get = Ember.get; | |
export default DS.JSONSerializer.extend({ | |
extractId(modelClass, resourceHash) { | |
var primaryKey = get(this, 'primaryKey'); | |
var id = resourceHash[primaryKey]; | |
return parseInt(coerceId(id), 10); | |
} | |
}); |
This file contains 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
// app/services/store.js | |
import DS from 'ember-data'; | |
import coerceId from "ember-data/-private/system/coerce-id"; | |
import { assert } from "ember-data/-private/debug"; | |
export default DS.Store.extend({ | |
_internalModelForId(modelName, inputId) { | |
// heimdall.increment(this._internalModelForId); | |
let modelClass = this.modelFor(modelName); | |
let id = parseInt(coerceId(inputId), 10); | |
let idToRecord = this.typeMapFor(modelClass).idToRecord; | |
let internalModel = idToRecord[id]; | |
if (!internalModel || !idToRecord[id]) { | |
internalModel = this.buildInternalModel(modelClass, id); | |
} | |
return internalModel; | |
}, | |
updateId(internalModel, data) { | |
var oldId = internalModel.id; | |
var id = parseInt(coerceId(data.id), 10); | |
assert("An adapter cannot assign a new id to a record that already has an id. " + internalModel + " had id: " + oldId + " and you tried to update it with " + id + ". This likely happened because your server returned data in response to a find or update that had a different id than the one you sent.", oldId === null || id === oldId); | |
this.typeMapFor(internalModel.type).idToRecord[id] = internalModel; | |
internalModel.setId(id); | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment