Skip to content

Instantly share code, notes, and snippets.

@samselikoff
Created October 16, 2017 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samselikoff/6c3b6992cfaf9d0844cad13f0350dde9 to your computer and use it in GitHub Desktop.
Save samselikoff/6c3b6992cfaf9d0844cad13f0350dde9 to your computer and use it in GitHub Desktop.
import './ext/reopen-ember-data-relationships';
import Ember from 'ember';
import DS from 'ember-data';
/*
Force all associations to be async: false.
*/
let originalBelongsTo = DS.belongsTo;
DS.belongsTo = function(...args) {
let options = args.find(arg => typeof arg === 'object');
let newArgs = [...args];
let newOptions = { async: false };
if (options) {
Ember.assert(`We're monkey patching Ember Data to force sync relationships! You have a '${arguments[0]}' relationship defined somewhere with an 'async' option. Please remove it.`, options.async === undefined);
let optionsIndex = args.indexOf(options);
newOptions = Ember.assign({}, options, newOptions);
newArgs.splice(optionsIndex, 1, newOptions);
} else {
newArgs.push(newOptions);
}
return originalBelongsTo(...newArgs);
};
let originalHasMany = DS.hasMany;
DS.hasMany = function(...args) {
let options = args.find(arg => typeof arg === 'object');
let newArgs = [...args];
let newOptions = { async: false };
if (options) {
Ember.assert(`We're monkey patching Ember Data to force sync relationships! You have a '${arguments[0]}' relationship defined somewhere with an 'async' option. Please remove it.`, options.async === undefined);
let optionsIndex = args.indexOf(options);
newOptions = Ember.assign({}, options, newOptions);
newArgs.splice(optionsIndex, 1, newOptions);
} else {
newArgs.push(newOptions);
}
return originalHasMany(...newArgs);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment