Permalink
Browse files

fixes problem with delegate being removed within another delegate

  • Loading branch information...
1 parent d059bfd commit 8fa6e3c62d134ef1080885d781d980dddd56bcba @justinbmeyer justinbmeyer committed Feb 22, 2012
Showing with 33 additions and 6 deletions.
  1. +4 −6 lang/observe/delegate/delegate.js
  2. +29 −0 lang/observe/delegate/delegate_test.js
@@ -51,26 +51,23 @@ steal('jquery/lang/observe',function(){
delegate = function(event, prop, how, newVal, oldVal){
// pre-split properties to save some regexp time
var props = prop.split("."),
- delegates = $.data(this,"_observe_delegates") || [],
+ delegates = ($.data(this,"_observe_delegates") || []).slice(0),
delegate,
- len = delegates.length,
attr,
matchedAttr,
hasMatch,
valuesEqual;
event.attr = prop;
event.lastAttr = props[props.length -1 ];
-
// for each delegate
- for(var i =0; i < len; i++){
+ for(var i =0; delegate = delegates[i++];){
- delegate = delegates[i];
// if there is a batchNum, this means that this
// event is part of a series of events caused by a single
// attrs call. We don't want to issue the same event
// multiple times
// setting the batchNum happens later
- if(event.batchNum && delegate.batchNum === event.batchNum){
+ if((event.batchNum && delegate.batchNum === event.batchNum) || delegate.undelegated ){
continue;
}
@@ -294,6 +291,7 @@ steal('jquery/lang/observe',function(){
delegateOb = delegates[i];
if( delegateOb.callback === cb ||
(!cb && delegateOb.selector === selector) ){
+ delegateOb.undelegated = true;
delegates.splice(i,1)
} else {
i++;
@@ -221,4 +221,33 @@ test("compound sets", function(){
equals(count, 3, "setting person does not fire anything");
})
+test("undelegate within event loop",1, function(){
+
+ var state = new $.Observe({
+ type : "person",
+ id: "5"
+ });
+ var f1 = function(){
+ state.undelegate("type","add",f2);
+ },
+ f2 = function(){
+ ok(false,"I am removed, how am I called")
+ },
+ f3 = function(){
+ state.undelegate("type","add",f1);
+ },
+ f4 = function(){
+ ok(true,"f4 called")
+ };
+ state.delegate("type", "set", f1);
+ state.delegate("type","set",f2);
+ state.delegate("type","set",f3);
+ state.delegate("type","set",f4);
+ state.attr("type","other");
+
+})
+
+
+
+
});

0 comments on commit 8fa6e3c

Please sign in to comment.