|
3 | 3 | * |
4 | 4 | */ |
5 | 5 | (function ( $, undefined ) { |
6 | | - $.observable = function( data, parent ) { |
7 | | - return new observable( data, parent ); |
| 6 | + $.observable = function ( data ) { |
| 7 | + return new observable( data ); |
8 | 8 | }; |
9 | 9 |
|
10 | 10 | var splice = [].splice; |
11 | 11 |
|
12 | | - function observable( data, parent ) { |
| 12 | + var observable = $.observable.Observable = function ( data ) { |
13 | 13 | this.data = data; |
14 | | - this.parent = parent; |
15 | 14 | } |
| 15 | + |
16 | 16 | observable.prototype = { |
17 | 17 | data: null, |
18 | 18 |
|
|
46 | 46 | changed = true; |
47 | 47 | oldValues[ key ] = oldValue; |
48 | 48 | newValues[ key ] = path[ key ]; |
49 | | - this._set( key, path[ key ] ); |
50 | 49 | } |
51 | 50 | } |
52 | 51 | if ( changed ) { |
53 | | - this._trigger( "change", { |
54 | | - oldValues: oldValues, |
55 | | - newValues: newValues |
56 | | - }); |
| 52 | + return this._property( oldValues, newValues ); |
57 | 53 | } |
58 | 54 | } else if (arguments.length == 1) { |
59 | 55 | return this._get( path ); |
60 | 56 | } else { |
61 | 57 | var oldValue = this._get( path ); |
62 | 58 | // TODO should be strict? currently helpers are unaware of data types, don't do parsing, therefore strict comparison wouldn't be good |
63 | 59 | if ( oldValue != value ) { |
64 | | - this._set( path, value ); |
65 | 60 | var oldValues = {}; |
66 | 61 | oldValues[ path ] = oldValue; |
67 | 62 | var newValues = {}; |
68 | 63 | newValues[ path ] = value; |
69 | | - this._trigger( "change", { |
70 | | - oldValues: oldValues, |
71 | | - newValues: newValues |
72 | | - }); |
| 64 | + return this._property( oldValues, newValues ); |
73 | 65 | } |
74 | 66 |
|
75 | 67 | } |
76 | 68 | return this; |
77 | 69 | }, |
78 | 70 |
|
| 71 | + _property: function( oldValues, newValues ) { |
| 72 | + var that = this; |
| 73 | + $.each( newValues, function( path, value ) { |
| 74 | + that._set( path, value ); |
| 75 | + } ); |
| 76 | + |
| 77 | + return this._trigger( "change", { |
| 78 | + oldValues: oldValues, |
| 79 | + newValues: newValues |
| 80 | + } ); |
| 81 | + }, |
| 82 | + |
79 | 83 | insert: function( index, items) { |
80 | 84 | // insert( object ) |
81 | 85 | if ( $.type(index) === "object" ) { |
|
85 | 89 | } else if ( !$.isArray( items ) ) { |
86 | 90 | items = [ items ]; |
87 | 91 | } |
| 92 | + |
| 93 | + return this._insert( index, items ); |
| 94 | + }, |
| 95 | + |
| 96 | + _insert: function( index, items ) { |
88 | 97 | // insert( index, objects ) |
89 | 98 | splice.apply( this.data, [ index, 0 ].concat( items ) ); |
90 | 99 | return this._trigger( "insert", { |
|
135 | 144 | if ( !numToRemove ) { |
136 | 145 | numToRemove = 1; |
137 | 146 | } |
| 147 | + |
| 148 | + return this._remove( index, numToRemove ); |
| 149 | + }, |
| 150 | + |
| 151 | + _remove: function( index, numToRemove ) { |
138 | 152 | var items = this.data.slice( index, index + numToRemove ); |
139 | 153 | this.data.splice( index, numToRemove ); |
140 | 154 | // TODO update event data, along with support for removing array of objects |
|
0 commit comments