Dojo 상점의 경우 특정 코드를 put()
및 add()
으로 실행하고 싶습니다. 나는 데 문제는 JSON REST 저장을 위해, JsonRest.js에 add()
이 put()
를 호출 단지 함수이다 : 나는 add()
와 aspect.around()
를 사용하는 경우, 그래서aspect.around를 사용하고 있지만 서로를 호출하는 메소드를 확인하십시오.
add: function(object, options){
options = options || {};
options.overwrite = false;
return this.put(object, options);
},
, 내 코드는 두 번 실행되는 끝 IFadd()
을 스텁으로 구현하는 상점에서 생성 된 상점에 put()
으로 코드를 적용합니다.
대부분의 상점에서 그렇게 할 것임을 알고 있습니다. 메서드가 중첩되어 있는지 여부와 상관없이 내 솔루션이 스토어와 작동하도록 보장하기 만하면됩니다.
Dojo 자신의 Observable.js
에 동일한 문제가 있습니다.
function whenFinished(method, action){
var original = store[method];
if(original){
store[method] = function(value){
if(inMethod){
// if one method calls another (like add() calling put()) we don't want two events
return original.apply(this, arguments);
}
inMethod = true;
try{
var results = original.apply(this, arguments);
Deferred.when(results, function(results){
action((typeof results == "object" && results) || value);
});
return results;
}finally{
inMethod = false;
}
};
}
}
// monitor for updates by listening to these methods
whenFinished("put", function(object){
store.notify(object, store.getIdentity(object));
});
whenFinished("add", function(object){
store.notify(object);
});
whenFinished("remove", function(id){
store.notify(undefined, id);
});
내 질문은 : 그것은 방법 내에 있는지 확인하도록 기존 코드를 변경하고, 두 번 코드를 실행하지 않도록하는 간단한, "짧은"방법이이 그들이 그것을 다루는 방법입니까?
나는 그것을 줬다. 그러나 나는 clanky, hacky code로 끝났다. 내가 뭔가를 놓친 거지 확신 ... 여기
기존 코드입니다 : 이것은 내 시도가 ...
topic.subscribe('hotplate/hotDojoStores/newStore', function(storeName, store){
aspect.around(store, 'put', function(put){
return function(object, options){
return when(put.call(store, object, options)).then(function(r) {
var eventName;
var identity = store.idProperty;
eventName = object[ identity ] ? 'storeRecordUpdate' : 'storeRecordCreate';
topic.publish(eventName, null, { type: eventName, storeName: storeName, objectId: r[ identity ], object: object }, false);
});
}
});
aspect.around(store, 'add', function(add){
return function(object, options){
return when(add.call(store, object, options)).then(function(r) {
var identity = store.idProperty;
topic.publish('storeRecordCreate', null, { storeName: storeName, storeTarget: storeTarget, objectId: r[identity], object: object }, false } );
});
}
});
});
나는 곡물을 따라 Observable이 내 저장소 랩퍼에서하는 일을 수행합니다. [put caller is add]를 확인하여 "더 짧은"접근 방식이있을 수 있습니다 (http://stackoverflow.com/questions/280389/how-do-you-find-out-the-caller-function-in-javascript) -하지만 피곤하고 그것을 방지하는 측면 때문에 스택 체조 선수가 필요할 수도 있습니다. – bishop
아내가 내가 바라던 대답이 아니었다 ... 나의 시도 된 답을 볼 수 있니? 그것은 제정신이 아닌가요? – Merc