在使用Ember CLI时,有人可以向我解释如何进行猴子修补吗?这是关于以下方法:
BelongsToRelationship.prototype.addRecord = function(newRecord) {
if (this.members.has(newRecord)){ return;}
var type = this.relationshipMeta.type;
Ember.assert("You can only add a '" + type.typeKey + "' record to this relationship", newRecord instanceof type);
if (this.inverseRecord) {
this.removeRecord(this.inverseRecord);
}
this.inverseRecord = newRecord;
this._super$addRecord(newRecord);
};
我希望暂时禁用assert语句但是我没有看到如何在不分支整个项目的情况下执行此操作。然后我还需要分叉ember-data bower包项目然后创建我自己的发行版。
- 托马斯
答案 0 :(得分:1)
BelongsToRelationship
的定义方式不能通过全局范围访问。但是,您可以通过实例化具有DS.belongsTo
关系的模型并在其中挖掘来检索它。
这个初始化器应该可以解决这个问题:
// app/initializers/monkey-patch.js
import DS from 'ember-data';
import Ember from 'ember';
//Hacky way to get BelongsToRelationship constructor
var getBelongsToConstructor = function(store) {
var dummyRecord = store.createRecord(DS.Model.extend({
dummy: DS.belongsTo(DS.Model.extend())
}));
return dummyRecord._relationships.dummy.constructor;
};
export default {
name: 'monkey-patch',
after: 'store',
initialize: function(container, application) {
var store = container.lookup('store:main'),
BelongsToRelationship = getBelongsToConstructor(store),
originalAssert = Ember.assert,
originalAddRecord = BelongsToRelationship.prototype.addRecord;
//Overriding assert in order to check Ember.disableAssert flag
Ember.assert = function() {
var args = Array.prototype.slice.call(arguments, 0),
retVal;
if (!Ember.disableAssert) {
retVal = originalAssert.apply(this, args);
}
return retVal;
};
//Overriding BelongsToRelationship - calling super while asserion is disabled
BelongsToRelationship.prototype.addRecord = function() {
var args = Array.prototype.slice.call(arguments, 0),
retVal;
Ember.disableAssert = true;
retVal = originalAddRecord.apply(this, args);
Ember.disableAssert = false;
return retVal;
};
}
};