@ -35,7 +35,7 @@ function DataView (data, options) {
* @ param { DataSet | DataView } data
* @ param { DataSet | DataView } data
* /
* /
DataView . prototype . setData = function ( data ) {
DataView . prototype . setData = function ( data ) {
var ids , id , i , len ;
var ids , id , i , len , items ;
if ( this . _data ) {
if ( this . _data ) {
// unsubscribe from current dataset
// unsubscribe from current dataset
@ -44,10 +44,16 @@ DataView.prototype.setData = function (data) {
}
}
// trigger a remove of all items in memory
// trigger a remove of all items in memory
ids = Object . keys ( this . _ids ) ;
ids = this . _data . getIds ( { filter : this . _options && this . _options . filter } ) ;
items = [ ] ;
for ( i = 0 , len = ids . length ; i < len ; i ++ ) {
items . push ( this . _data . _data [ ids [ i ] ] ) ;
}
this . _ids = { } ;
this . _ids = { } ;
this . length = 0 ;
this . length = 0 ;
this . _trigger ( 'remove' , { items : ids } ) ;
this . _trigger ( 'remove' , { items : ids , oldData : items } ) ;
}
}
this . _data = data ;
this . _data = data ;
@ -80,18 +86,19 @@ DataView.prototype.setData = function (data) {
* /
* /
DataView . prototype . refresh = function ( ) {
DataView . prototype . refresh = function ( ) {
var id , i , len ;
var id , i , len ;
var ids = this . _data . getIds ( { filter : this . _options && this . _options . filter } ) ;
var oldIds = Object . keys ( this . _ids ) ;
var newIds = { } ;
var added = [ ] ;
var removed = [ ] ;
var ids = this . _data . getIds ( { filter : this . _options && this . _options . filter } ) ,
oldIds = Object . keys ( this . _ids ) ,
newIds = { } ,
addedIds = [ ] ,
removedIds = [ ] ,
removedItems = [ ] ;
// check for additions
// check for additions
for ( i = 0 , len = ids . length ; i < len ; i ++ ) {
for ( i = 0 , len = ids . length ; i < len ; i ++ ) {
id = ids [ i ] ;
id = ids [ i ] ;
newIds [ id ] = true ;
newIds [ id ] = true ;
if ( ! this . _ids [ id ] ) {
if ( ! this . _ids [ id ] ) {
added . push ( id ) ;
addedIds . push ( id ) ;
this . _ids [ id ] = true ;
this . _ids [ id ] = true ;
}
}
}
}
@ -100,19 +107,20 @@ DataView.prototype.refresh = function () {
for ( i = 0 , len = oldIds . length ; i < len ; i ++ ) {
for ( i = 0 , len = oldIds . length ; i < len ; i ++ ) {
id = oldIds [ i ] ;
id = oldIds [ i ] ;
if ( ! newIds [ id ] ) {
if ( ! newIds [ id ] ) {
removed . push ( id ) ;
removedIds . push ( id ) ;
removedItems . push ( this . _data [ id ] ) ;
delete this . _ids [ id ] ;
delete this . _ids [ id ] ;
}
}
}
}
this . length += added . length - removed . length ;
this . length += addedIds . length - removedIds . length ;
// trigger events
// trigger events
if ( added . length ) {
if ( addedIds . length ) {
this . _trigger ( 'add' , { items : added } ) ;
this . _trigger ( 'add' , { items : added } ) ;
}
}
if ( removed . length ) {
this . _trigger ( 'remove' , { items : removed } ) ;
if ( removedIds . length ) {
this . _trigger ( 'remove' , { items : removedIds , oldData : removedItems } ) ;
}
}
} ;
} ;
@ -298,14 +306,14 @@ DataView.prototype.getDataSet = function () {
DataView . prototype . _onEvent = function ( event , params , senderId ) {
DataView . prototype . _onEvent = function ( event , params , senderId ) {
var i , len , id , item ;
var i , len , id , item ;
var ids = params && params . items ;
var ids = params && params . items ;
var data = this . _data ;
var updatedData = [ ] ;
var oldData = [ ] ;
var added = [ ] ;
var updated = [ ] ;
var removed = [ ] ;
if ( ids && data ) {
var addedIds = [ ] ,
updatedIds = [ ] ,
removedIds = [ ] ,
oldItems = [ ] ,
updatedItems = [ ] ,
removedItems = [ ] ;
if ( ids && this . _ data) {
switch ( event ) {
switch ( event ) {
case 'add' :
case 'add' :
// filter the ids of the added items
// filter the ids of the added items
@ -314,7 +322,7 @@ DataView.prototype._onEvent = function (event, params, senderId) {
item = this . get ( id ) ;
item = this . get ( id ) ;
if ( item ) {
if ( item ) {
this . _ids [ id ] = true ;
this . _ids [ id ] = true ;
added . push ( id ) ;
addedIds . push ( id ) ;
}
}
}
}
@ -329,19 +337,20 @@ DataView.prototype._onEvent = function (event, params, senderId) {
if ( item ) {
if ( item ) {
if ( this . _ids [ id ] ) {
if ( this . _ids [ id ] ) {
updated . push ( id ) ;
updatedData . push ( params . data [ i ] ) ;
oldData . push ( params . oldData [ i ] ) ;
updatedIds . push ( id ) ;
updatedItems . push ( params . data [ i ] ) ;
oldItems . push ( params . oldData [ i ] ) ;
}
}
else {
else {
this . _ids [ id ] = true ;
this . _ids [ id ] = true ;
added . push ( id ) ;
addedIds . push ( id ) ;
}
}
}
}
else {
else {
if ( this . _ids [ id ] ) {
if ( this . _ids [ id ] ) {
delete this . _ids [ id ] ;
delete this . _ids [ id ] ;
removed . push ( id ) ;
removedIds . push ( id ) ;
removedItems . push ( params . oldData [ i ] ) ;
}
}
else {
else {
// nothing interesting for me :-(
// nothing interesting for me :-(
@ -357,23 +366,24 @@ DataView.prototype._onEvent = function (event, params, senderId) {
id = ids [ i ] ;
id = ids [ i ] ;
if ( this . _ids [ id ] ) {
if ( this . _ids [ id ] ) {
delete this . _ids [ id ] ;
delete this . _ids [ id ] ;
removed . push ( id ) ;
removedIds . push ( id ) ;
removedItems . push ( params . oldData [ i ] ) ;
}
}
}
}
break ;
break ;
}
}
this . length += added . length - removed . length ;
this . length += addedIds . length - removedIds . length ;
if ( added . length ) {
this . _trigger ( 'add' , { items : added } , senderId ) ;
if ( addedIds . length ) {
this . _trigger ( 'add' , { items : addedIds } , senderId ) ;
}
}
if ( updated . length ) {
this . _trigger ( 'update' , { items : updated , oldData : oldData , data : updatedData } , senderId ) ;
if ( updatedIds . length ) {
this . _trigger ( 'update' , { items : updatedIds , oldData : oldItems , data : updatedItems } , senderId ) ;
}
}
if ( removed . length ) {
this . _trigger ( 'remove' , { items : removed } , senderId ) ;
if ( removedIds . length ) {
this . _trigger ( 'remove' , { items : removedIds , oldData : removedItems } , senderId ) ;
}
}
}
}
} ;
} ;