Created
November 3, 2011 11:07
-
-
Save ppcano/1336271 to your computer and use it in GitHub Desktop.
Notify ArrayObserves that array has been updated
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
require('sproutcore'); | |
require('sproutcore-ui'); | |
var set = SC.set, get = SC.get; | |
var max = 100 | |
, i = 0 | |
, name = 'name' | |
, controller | |
, items = []; | |
// inspired : runtime/tests/mixin/array_test.js | |
CustomObserver = SC.Object.extend({ | |
willCount: 0, | |
didCount: 0, | |
arrayWillChange: function() { | |
this.willCount++; | |
}, | |
arrayDidChange: function() { | |
this.didCount++; | |
} | |
}); | |
CustomController = SC.ArrayController.extend({ | |
content: null, | |
sortedContent:null, | |
_orderContent: function() { | |
var content = get(this, 'content'); | |
if ( content ) { | |
var result = SC.copy( content ); | |
result = result.sort( function( a, b ) { | |
return a.get('position') - b.get('position'); | |
}); | |
set(this, 'sortedContent', result ); | |
// TODO get(this, 'sortedContent') | |
// notifyPropertyChange // contentWillChange // contentDidChange | |
} | |
}.observes('@each.position') | |
}); | |
Item = SC.Object.extend({ | |
position: null, | |
name: null | |
}); | |
function _createObject( position ) { | |
return Item.create( { position: position, name: name , origin: position} ); | |
} | |
module("Live Items Controller test", { | |
setup: function() { | |
console.group(' - Setup for new test'); | |
for(i=0; i< max; i++) { | |
items.push( _createObject(i) ); | |
} | |
SC.run(function() { | |
controller = Client.LiveItemsController.create({ | |
content: items | |
}); | |
}); | |
}, | |
teardown: function() { | |
items = []; | |
controller.destroy(); | |
console.groupEnd(); | |
} | |
}); | |
test(" should notify only once when removed element from the array ", function() { | |
var controller1 = CustomController.create({ | |
content: items | |
}); | |
var observer = CustomObserver.create({}); | |
// this observes simulates the view which observes | |
controller1.get('sortedContent').addArrayObserver(observer); | |
SC.run(function() { | |
items.removeAt(0); | |
}); | |
equals(observer.didCount, 1, " should be notified once"); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://groups.google.com/group/sproutcore/browse_thread/thread/6dcc1fbb62343c28?hl=en