Skip to content

Instantly share code, notes, and snippets.

@ppcano
Created November 2, 2011 19:51
Show Gist options
  • Save ppcano/1334693 to your computer and use it in GitHub Desktop.
Save ppcano/1334693 to your computer and use it in GitHub Desktop.
SC2: how can notify array was modified on a elegant way?
CustomArrayController = 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');
});
var currentSortedContent = get(this, 'sortedContent')
if (!currentSortedContent) currentSortedContent = [];
var i=0
, currentMax = currentSortedContent.length
, max = result.length;
for ( i=0; i<currentMax; i++) {
currentSortedContent.popObject();
}
for ( i=0; i<max; i++) {
currentSortedContent.pushObject( result[i] );
}
set(this, 'sortedContent', currentSortedContent );
// TODO
// notifyWillChange
// set(this, 'sortedContent', result );
// notifyDidChange
}
}.observes('@each.position')
});
@ppcano
Copy link
Author

ppcano commented Nov 2, 2011

looking for a optimal way, something like the TODO comment should be required)

@ppcano
Copy link
Author

ppcano commented Nov 2, 2011

see how can fit: array.notifyPropertyChange('[]') ----> stilll pending

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment